Search code examples
cpointersstructmemory-addresspointer-to-pointer

In C can expressions be pointers?


struct node
{
       int a;
};


int main()
{
    struct node y = {24};
    struct node *x = &y;
 
    return 0;
}

I have recently been having trouble to see how the expression &x is a pointer to pointer type (struct **node) but after doing a little research i found out that since a pointer is just a object that holds (or points to) a memory address,then the expression &x is technically a pointer since it yields a address (with the actual expression &x the pointer that can be used in code).

&x is technically of type pointer to pointer because &x is a pointer to the address of x, the yielding of the address of x gives us another pointer aka the actual pointer x, hence it being pointer to pointer.

My question is, in C how can expressions be used as a pointer? Am i correct to assume that &x can be used in code as a pointer, after all it is just an expression, in memory how does it look? Maybe i am overthinking and the matter is as simple as & returning a pointer to its operand. Lastly is my understanding correct of how it is a pointer to pointer?


Solution

  • From this Draft C11 Standard:

    6.5.3.2 Address and indirection operators


    Semantics
    3    The unary & operator yields the address of its operand. If the operand has type “type”, the result has type “pointer to type”. …