arrays representation
Why do we pass an array like a[][10] in this format
Hello @vanshu21,
When you pass an array as an argument to a function, it will decay into a pointer to the first element of the array.
int intArray[3]
// Decays into
int * intArray;
Which are equivalent in the mind of the compiler. When you refer to an array by index, the compiler will actually use pointer arithmetic to determine which slot of memory to go to.
Now with a 2D array, it actually decays into a pointer to the first element as well. If the array is declared to be on the stack, the 2D storage is actually in Row Major format. The compiler needs to know the column value in order to perform pointer arithmetic to find the element you are looking for. C++ arrays are not objects and this is why there are no length checks for arrays in C++.
A two-dimensional array in C++ is really an array of arrays. So the parameter you’re passing is a pointer to the array containing the first column.
To access the second column, the pointer must be incremented past the first column. To do that the compiler must know the size of the column, just as it must know the size of any other array element.
Hope, this would help.
Give a like, if you are satisfied.