I’m interested in whether an ‘unsigned long long’ variable can be treated as an address, and the result seemed it was correct?
int var = 0;
// the variable var's address is 0x7fffffffe4a4 now in my system
unsigned long long ull = 0x7fffffffe4a4;
scanf("%d", ull); // I try to use ull as var's address and assign a value to var
printf("%d\n", var); // It seemingly works!
And I also conducted an experiment as follows.
int var = 0;
// the variable var's address is 0x7fffffffe4a4 now in my system
unsigned long long ull = 0x7fffffffe4a4;
int *ptr = &var;
// First, I want to know whether the memory used by unsigned long long and the one used by a pointer differ
printf("%zu\n", sizeof(ull));
printf("%zu\n", sizeof(ptr));
// The results are both "8" in my system
// Next I try to assign two different values to var by two ways
scanf("%d", ull); // I try to use x as an address to assign var
printf("%d\n", var);
scanf("%d", ptr); // And I also use a normal pointer to assign var
printf("%d\n", var);
// The results were in line with expectations!
Well, it seems an ‘unsigned long long’ variable can be used as a pointer successfully(although the compiler warned me that the argument was expected to be ‘int*’ rather than ‘unsigned long lnog’), and I wonder what’s the difference between a variable and a pointer at the hardware level, how these two types of objects are processed when they are stored and used, who processes these objects and whether it’s recommended to perform the operations above.
(In the end Is it the feature of c? 🙂 )
>Solution :
Here is a good analogy: using numbers instead of pointers is not wrong, it’s dangerous. Imagine at the reason why you don’t use a bicycle on a highway. Is it because you can’t ride it? Of course you can, you have wheels, the asphalt is good. You don’t do it because it’s dangerous, the bicycle is not designed to do it.
For a more formal explanation:
Congratulations, you discovered that addresses are, ultimately, just numbers. The reason why in languages such as C or C++ (they are different!!) pointers are introduced is to avoid confusion and errors, letting the users and the compiler know specifically when they are dealing with an address or a number.
As I said at the beginning, at the end of the day, an address is a number that needs to tell the hardware where to look in the memory. However a pointer is a number that is treated with special care by the compiler:
-
You have the guarantee that a pointer has enough bits to store the address. Your example works "fine" with 64bits systems, but in a 16bits system a pointer will have the same range as a
unsigned shortvalue, and if you try to assign aunsigned longyou will run into all sorts of problems. -
Arithmetic on pointers follow the size of the underlying pointed data type. When you are pointing at a
shortat address0x100and you want to go to the next byte, you know that you have to look at ‘0x102’.
short* ps = (short*)0x100;
ps += 1; // ps is now 0x102, because the compiler knows that short is 2 bytes
*ps = 0xAABB;
short s = 0x100;
// I need to advance to the next short.. mmm I have to do this:
s += sizeof(short).
*((short*)s) = 0xAABB; // Also, ugly syntax.
// See how easy it is to make errors when you use plain numbers?
Bottom line: pointers are special numbers with special properties, especially thought to handle memory accesses and address arithmetic.