Search code examples
pointersdynamicinitializationdereferenceindirection

how do i initialize a pointer with 3 levels of indirection? c++


shaded boxes are pointers and elements that hold the values are on the heap

diagram

I pretty much need to come up with the statements to implement the diagram. I am so thrown off by initializing something pointed to with 3 levels of indirection that is on the heap.

please help me... this is what i have

int *c{ new int(18) },
 ***a{ new int**(&c) },
 *e{ new int(22) },
 **b;
b = &e;

    b = nullptr;
e = nullptr;
a = nullptr;
c = nullptr;

delete b, e, a, c;

Solution

  • Figured it out.

    Example for the pointer a.

    int*** a;
    a = new int**;
    *a = new int*;
    **a = new int(18);