I'm trying to have three levels of pointer indirection with an additional pointer pointing to level two of the indirection. This is for a class, and I'm having some real issues. This is what I'm doing.
int ***s = new int **[row];
*s = new int *[row];
**s = new int[row];
Now if these where just int and not arrays I could do,
***s = 1;
To store it into the yellow square on my picture, but I don't know how to access the array elements, I have tried a few things and it either crashes or will not compile. Any help, even pointing me in the right direction would be very useful. Thank you.
You've created something like this (assume row
is 2 and type T
):
T*** +-----+ | | +--/--+ / / T** +--/--+-----+ | | | +-----+--+--+ -/ | --/ T* | +--/--+-----+ +-+---+-----+ | | | | | | +-----+-----+ +--+--+-----+ ----/ -/ | \--- -----/ --/ T | \-- +--/--+-----+ +--/--+-----+ +--+--+-----+ +--\--+-----+ | | | | X | | | | | | | | +-----+-----+ +-----+-----+ +-----+-----+ +-----+-----+
Every node would point to the next level's first node. Dereferencing every level would give the level next but you've to also take care of the index of the element in the array you want to reach. This, I see, isn't done in your code. For scalars you dereference using *
while for arrays the array index syntax does dereferencing too apart from choosing the right element. *
on arrays would just get you the first element always.
To access X
in the above figure you'd do
T** v = u[0];
T* w = v[1];
T x = w[0];
// shorthand for above
x = u[0][1][0];
To just have an array at the last level, you should be doing this
int*** p = new int**;
*p = new int*;
**p = new int[row];
This would just give you █ → █ → █ → █ █ █ …, where p
itself (the first box) is an automatic variable (usually stored in the stack space) and rest comes from the freestore (usually living in the heap).