Search code examples
c++pointersstructmembermemory-address

Pointer points to another pointer in structure


I have structure named temp with member n:

struct temp
{
    int n;
}

I wish to declare a pointer p to access member n.

struct temp *p

How to access member n by another pointer p1 which points to pointer p.


Solution

  • You can access member n through a pointer to p with

    temp a;
    temp *p = &a;
    temp **p1 = &p;
    (*p1)->n = 2;
    

    p1 is a pointer to p. Dereferencing it gives a reference to p. (*p1) and p are equivalent.