I am unable to get my head around what type of value does the deference operator accept as its argument. By value, here I mean is it lvalue or rvalue?
Consider the following code snippet:
int i = 1, *p = &i;
cout << *p++; // Prints 1, so far so good.
Now since postfix ++
returns the unincremented value (unlike its prefix counterpart which returns incremented object) of p
, from the code above it appears that unary *
accepts an rvalue as its only operand. But,
cout << *i; // error: invalid type argument of unary ‘*’ (have ‘int’)
Okay, maybe the value of i
doesn't represent a valid memory address. Now to confirm that unary *
can work on an rvalue that represents a valid memory location, I tried something like this:
unsigned long add = p; // error: invalid conversion from ‘int*’ to ‘long unsigned int’ [-fpermissive]
cout << *add; // error: invalid type argument of unary ‘*’ (have ‘int’)
This also brings me to my next question. Assuming that the size of unsigned long
is 64 bits (which happens to be true for the system I am working on), why is it not possible to copy the value stored in p
(which happens to be a 64-bit memory address) into add
? Or in other words, is there a way to store a memory address in a variable of non-pointer type (I would be surprised if this isn't possible because a memory address is after all also a value)?
Thanks