C supports arrays with any number of dimensions: int cube[2][3][4]. Each extra pair of brackets adds a level, and elements are stored in row-major order with the last index changing fastest. Large multidimensional arrays use a lot of memory, so check sizes.
Remember
int cube[2][2][2]; /* 2 planes, 2 rows, 2 cols */
cube[1][0][1] = 5; /* plane 1, row 0, col 1 */
total = sizeof cube; /* 8 * sizeof(int) */
Reference
Cheat Sheet
Keep these facts handy while you work with Multidimensional Arrays in C.
Topic
Detail
Declare
type a[d1][d2][d3];
Fastest index
The last one
Elements
d1 * d2 * d3
Function parameter
Give all but the first size
Code
C Examples
Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Variable Length Arrays.
Example 1 — Fill and read a 3D array
Values count up in memory order.
C
#include <stdio.h>
int main(void) {
int cube[2][2][2];
int n = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
cube[i][j][k] = n++;
printf("cube[1][0][1] = %d\n", cube[1][0][1]);
printf("elements = %zu\n", sizeof cube / sizeof cube[0][0][0]);
printf("bytes = %zu\n", sizeof cube);
return 0;
}
Output
cube[1][0][1] = 5
elements = 8
bytes = 32
How It Works
The innermost index k varies fastest, so values 0..7 fill in order.
cube[1][0][1] is plane 1 (4 elements in), row 0, column 1: 4 + 0 + 1 = 5.
8 ints at 4 bytes each is 32 bytes (typical platform).
Edge Cases & Pitfalls
Check these before relying on Multidimensional Arrays in real programs.
stack
Huge local arrays
A big local 3D array can overflow the stack. Use static or malloc for large data.
order
Loop order and speed
Make the innermost loop the last index so memory is read sequentially.
Remember
Key Takeaways
Layout: last index changes fastest.
Count: multiply the sizes.
Memory: large arrays belong on the heap or static storage.
One line: C supports arrays with any number of dimensions: int cube[2][3][4].
Frequently Asked Questions
The standard requires support for at least 12 array declarator levels, far more than practical code needs.
Use a single 1D array and compute the index, or an array of structs, when dimensions become hard to manage.
Continue with "C Variable Length Arrays" next. C99 arrays whose size is decided at run time. The C Arrays hub lists every topic in order.