Pointer to pointers (and array of pointers)

closed account (jvqpDjzh)
How to access correctly to pointers of a pointer (not an array of pointers, which is quite easily to understand the mechanism)

//to access to pointers of an array:

1
2
3
4
int* arrayOfIntPointers[3];

for(unsigned i = 0; i < 3; ++i)
    arrayOfIntPointers[i] = new int;


//but if we had a pointer to pointers? How can we "translate" the previous example?
Last edited on
The same way - arrays are basically just pointers (and will turn into pointers at the smallest opportunity). Example:
1
2
3
4
5
6
7
8
9
10
11
12
// pointer to pointer - allocate with new
int** pointerArrayOfIntPointers = new int*[3];
// allocate the values of the array
for (unsigned i = 0; i < 3; ++i)
    pointerArrayOfIntPointers[i] = new int;

// ... - use the array here somehow

// clean up to prevent memory leaks
for (unsigned i = 0; i < 3; ++i)
    delete pointerArrayOfIntPointers[i];
delete[] pointerArrayOfIntPointers;
Last edited on
Topic archived. No new replies allowed.