cpointersvariablespointer-address

Pointer and address in C


My teacher has these question on his lecture powerpoint but no answer. Can someone help. new to c.

If a is an int variable, is it always true that *&a == a ?
If p is an int* variable, is it always true that p == &*p ?
Is it ever meaningful to say **p ?
Is it ever meaningful to say &&a ?
After assigning a = 2 and p = &a, how much is *p**p ?
If furthermore q = &p, how much is **q**p***q ?

Finally, how much is a/*p ?


Solution

  • If a is an int variable, is it always true that *&a == a ?

    It should be, but I can't find a definitive statement to that effect.

    If p is an int* variable, is it always true that p == &*p ?

    Yes - in that case, neither the & nor * operators are actually evaluated. See the online draft of the C 2011 standard, § 6.5.3.2 ¶ 3.

    Is it ever meaningful to say **p ?

    Yes. Multiple indirection shows up all over the place. You can have pointers to pointers, pointers to pointers to pointers, pointers to arrays of pointers, pointers to functions returning pointers to arrays of pointers to pointers to pointers, etc.

    Is it ever meaningful to say &&a ?

    No. The result of &a is not an lvalue, so you cannot apply the & operator to it. To put it another way, what's the address of an address?

    After assigning a = 2 and p = &a, how much is *p**p ?

    Should be the same as a * a, although be aware that C's tokenizing algorithm is "greedy", so you really want to separate terms and operators with whitespace.

    If furthermore q = &p, how much is **q**p***q ?

    Again, it should be the same as a * a * a, and again, be aware of how C tokenizes expressions.