2D Array
2D array is a two-dimensional array in C++ , which we can imagine / commonly called as a matrix or grid having some fixed number of rows and columns that has to be defined at time of declaration , just like we define fixed size for a 1D array.
Syntax to declare an array:-
dataType arrayName[rows][columns];
where, rows and columns are fixed.
Defining a 2-D array
An array can be created using two ways:
By declaration
By initialization
By Declaration
#include <iostream>
int main() {
// Declare a 3x4 2D array
int array[3][4];
array[0][0]=1; //array[0][0] => 1st row 1st column
array[0][1]=1; //array[0][1] => 1st row 2nd column
array[0][2]=1; //array[0][2] => 1st row 3rd column
array[0][3]=1;
array[1][0]=1; //array[1][0] => 2nd row 1st column
array[1][1]=1; //array[1][1] => 2nd row 2nd column
array[1][2]=1;
array[2][0]=1; //array[2][0] => 3rd row 1st column
// Print the array
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) {
std::cout << array[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
//Output
1 1 1 1
1 1 1 31319
1 0 1651076199 779647075
In Declaration, the array is declared first and then after declaration values are assigned later.
The output of the program, without initializing all elements of the 2D array, is undefined behavior because it includes uninitialized variables.
C++ does not automatically initialize local variables (including arrays). The uninitialized elements will contain garbage values, which can be anything.
By Initialization
#include <iostream> int main() { // Declare a 3x4 2D array int array[3][4] = { {10, 20, 30, 40}, // Row 1 initialized {50,60}, // Row 2: Assign values to 2 elements {3, 8, 12} // Row 3: Assign values to 3 elements }; // Print the array for (int i = 0; i < 3; ++i) { for (int j = 0; j < 4; ++j) { std::cout << array[i][j] << " "; } std::cout << std::endl; } return 0; } //OUTPUT 10 20 30 40 50 60 0 0 3 8 12 0In Initialization, values are given at time of array declaration. So, the values which are not initialized like in row 2 and row 3 , they are by default initialized with 0.
#include <iostream>
int main()
{
int a[][4]= {1,1};
return 0;
}
//No error
In above program, if we declare 2D array like:
int a[][];
Or
int a[4][];
Then, we will get error.
error: declaration of 'a' as multidimensional , array must have bounds for all dimensions except the first
Address Calculation , representation of 2D array in memory and Base Address of 2D array

Actually, in C/C++, multi-dimensional arrays are stored in “Row Major” order. This means that in memory, it is stored array-by-array i.e. 1D array after 1D array row-wise.
This means that Row 0 is stored then Row 1 is stored and so on. Diagrammatically, its like: -

So, in memory , 2D array is actually stored as a 1D array. Thus, the memory blocks are allocated consecutively in a 2D array. This is why 1016 is the starting memory address of first memory block of row 2 , after the last memory block of row 1 which has starting memory address of 1012.
Base address of this 2D array is 1000. So, we can refer to whole 2D array by this base address.
a = &a[0] =&a[0][0]
array Name of 2D array is base address of 2D array.
Formula for address calculation of 2D array a[r][c] is :-

a[i][j] is interpreted by C++ compiler as:-
a[i][j] =( a[i]+j ) = (*(a+i) + j)
Internally, this a[i][j] expression is solved by above formula.
Example:-

So, here also the a[i][j] expression can be wriiten as:-
a[i][j] = j[ a[i] ]
Here, a+1 means array after 1 row skip (i.e. row 1 (2nd row) )
( a + i) *\=> the i-th row, which is itself an array (decays to a pointer to the first element in that row i) .
This array decay in C/C++ is happening because *(a+ i) = a[i] is a 1D array but array a is a 2D array , so, in C++, a[i] decays to a pointer pointing to row i.
Since pointer addition is commutative, we can write
a[i][j] == i[a][j] == j[ i[a] ] == j[ a[i] ]
and
a[i][j] ≠ a[j][i]
