C Multi-dimensional Arrays
In this page:
data_type array_name[rows][columns];
data_type array_name[rows][columns] = {{v1, v2}, {v3, v4}};
array_name[row][column];
Two-Dimensional Arrays
Internally, a 2D array is still stored as one contiguous block of memory in row-major order, meaning an entire row is stored before the next row begins -- this layout is why looping row-by-row is more cache-friendly than column-by-column.
Example: Two-Dimensional Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int grid[2][2] = {{1, 2}, {3, 4}};
// Print formatted text to the screen
printf("%d", grid[1][0]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Initializing 2D Arrays
When you initialize with nested braces like {{1,2},{3,4}}, each inner group fills one row in order; if you provide fewer values than a row needs, the remaining elements in that row are automatically set to zero.
Example: Initializing 2D Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int grid[2][2] = {{1, 2}, {3}};
// Print formatted text to the screen
printf("%d", grid[1][1]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing Elements in 2D Arrays
grid[2][3] reads as 'row 2, column 3' -- mixing up the order is a frequent source of bugs, especially when translating between mathematical matrix notation (which some fields write row-first, others column-first) and code.
Example: Accessing Elements in 2D Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int grid[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
// Print formatted text to the screen
printf("%d", grid[2][3]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modifying Elements in 2D Arrays
Just like 1D arrays, updating grid[i][j] only changes that single cell; there's no built-in way to reassign an entire row at once except by looping or using memcpy() on that row's contiguous memory.
Example: Modifying Elements in 2D Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int grid[2][2] = {{1, 2}, {3, 4}};
grid[0][1] = 99;
// Print formatted text to the screen
printf("%d", grid[0][1]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Looping Through 2D Arrays
The outer loop variable typically represents the row and the inner loop the column (or vice versa, as long as you're consistent) -- getting the loop bounds wrong for either dimension is easy to do and will either skip data or read out of bounds.
Example: Looping Through 2D Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int grid[2][2] = {{1, 2}, {3, 4}};
// Loop: repeat while the condition holds
for (int row = 0; row < 2; row++) {
// Loop: repeat while the condition holds
for (int col = 0; col < 2; col++) {
// Print formatted text to the screen
printf("%d ", grid[row][col]);
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Swapping row and column indexes, like
a[col][row]. - Omitting the column size when passing a 2D array to a function, which is required.
- Assuming a partially initialized 2D array is random, when the missing elements are 0.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: