It is common mistake that one might think that if int* i
== int i[5]
than int** i
== int i[5][5]
but it is not like that that for two reasons. First an int**
is a pointer to a pointer and has nothing to do with the ways arrays are laid out and secondly a 2D array is actually 1D array internally and what it does is lay the second arrays side by side.
That means that in an array of [2][2]
internally is creating an array of [4]
and when you try and access [0][0]
than that gets translated to [0]
, [0][1]
will get translated to [1]
, [1][0]
will get translated to [2]
and [1][1]
will get translated to [3]
Example:
#include
void Print(int* i, int element)
{
std::cout << i[element] << std::endl;
}
int main()
{
int i[5][5];
i[2][2] = 125;
Print((i[0]), 12);
}
This will print 125 because I set [2][2]
as 125 and that will be consider as equal to 12
because the first [0]
will occupy from 0 - 4, [1]
will occupy from 5-10 and so on and so forth.