When x is a L-value (let's say a variable), then the following identity holds:
x == *(&x)
This is quite easy to explain, because &x is a pointer to x and the dereference operator * applied to &x will then of course return x.
Now I am wondering if the converse makes sense. To be precise I am wondering if
p == &(*p)
when p is a non-dangling pointer. This seems to make sense, because *p is itself a L-value (a value which has an adress), because we already have the pointer (=adress) p to it. So you only need to know, that such pointers are unique, because then &(*p) has no other chance as to be p.
So when both identities are true you can say, that, mathematically, * and & are inverse functions of one another.
Am I correct? Are there any possible exceptions to this alleged rules?
When p
is an object pointer, &*p
is equivalent to p
. The evaluation of *p
doesn't occur and this is guaranteed by the C Standard.
char *q, *p = NULL;
q = &*p; // equivalent to q = p;
Here is the relevant paragraph of the Standard:
(C99, 6.5.3.2p3) "If the operand is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue."
EDIT: After @ldav1s comment, I changed the word pointer to object pointer. Indeed if p
is of type void *
, then &*p
is invalid. For information, this has been discussed by the C Committee in Defect Report #102: http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_012.html