Search code examples
c++dynamic-memory-allocationpointer-to-pointerpointer-to-array

What value does a pointer to pointer get assigned when points to a dynamically allocated memory?


Consider the following case:

int **my_array = new int*[10];
  1. What do we assign to my_array here?
  2. my_array is a pointer that points to what?
  3. Is there any way to iterate through my_array (the pointer) and set up a two-dimensional array of integers (and not int*)?

Solution

  • We assign to my_array the address of an array. The array contains pointers which can point to other arrays, but don't yet.

    Yes, we can do this:

    int **my_array = new int*[10];
    
    for(int i=0; i<10; ++i)
      my_array[i] = new int[13];
    
    my_array[2][11] = 500;