c++ - Confused with pointer and double pointer in the heap -


i'm getting confused pointers , double pointers.

suppose have these

int* ptr = new int[2]; 

here, ptr has address of array of size 2 in heap keeps int data right?

int** ptr = new int* [2]; 

here, ptr has address 2 pointers in heap , these 2 pointers can point int data.

is thinking here correct?

this correct: in first case assign pointer memory block in heap sufficient storing 2 ints, can considered int array of size two.

the second ptr assigned pointer memory block in heap sufficient storing 2 pointers int. however, these pointers not point in particular until assign them value*, in same way assign other pointers. example, can make second ptr make array of 2 int arrays, this:

int** ptr = new int* [2]; ptr[0] = new int[2]; ptr[1] = new int[2]; 

* same true ints first code snippet: value indeterminate until assign them.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

gradle error "Cannot convert the provided notation to a File or URI" -