Remember, there is no such thing as a 2D array.
And an int** does not point to a 2D array.
If you have a 9 element array, the elements are in memory as
1 2 3 4 5 6 7 8 9
and you can't tell if this is a [9][1] or a [3][3].
So when you allocate you allocate the number of elements.
Let's say you need a [4][6] array of int. That's 24 ints. So allocate 24 ints.
malloc will return a pointer to the 24 ints.
Now a [4][6] array is an array of 4 elements where each element is an array of 6 int. Therefore typecast the return from malloc as a pointer to an array of 6 int:
- int (*MyArray)[6] = (int(*)[6])malloc(24*sizeof(int));
You will need a separate variable to hold the 4 which is the number of elements.
Go back and read that article again. The final example is the one I am talking about here.