Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
3D Arrays in C
See a three-dimensional array as a stack of tables, count its elements, learn the flat-offset formula, and use the idea for multi-channel data in embedded C programming.
3D Arrays in C: Beyond Rows and Columns
3D arrays in C add a third index on top of rows and columns, so you can store a stack of tables instead of one table. They are less common than 1D and 2D arrays, but the moment you handle several sensors, several color planes or several time frames at once, the idea becomes natural. This lecture of our free C programming course explains the structure with diagrams, verifies the element count and memory layout with a real program, and shows how the same thinking applies when you read data in embedded C programming. It is also the last topic before the chapter quiz in this free embedded C course.
What You Will Learn
- How to read a declaration like int a[2][3][4]
- How to count elements and bytes with sizeof
- How to initialize a 3D array and access one element
- The offset formula that turns three indices into one memory position
- Practical embedded uses: multi-board, multi-channel and multi-frame data
- Stack-size and portability cautions
Prerequisites
Complete the earlier lectures on 1D arrays and 2D array initialization, and be comfortable with nested loops. The row-major layout idea from 2D arrays is the key requirement.
A Stack of Tables
The simplest picture: a three-dimensional array is an array of 2D arrays. Take this declaration:
int adc[2][3][4];
Read it from the left. There are 2 items, each of which is a 3×4 table (3 rows, 4 columns). We will give the indices meaning from an embedded example: the first index is the sensor board, the second is the channel on that board, and the third is the sample number. The syntax itself does not know about boards or channels; naming them is our choice, and it is what makes 3D arrays readable.
adc[2][3][4]: two boards, each holding a 3×4 table (channels x samples)
| adc[0] (board 0) | s0 | s1 | s2 | s3 |
|---|---|---|---|---|
| channel 0 | [0][0][0] | [0][0][1] | [0][0][2] | [0][0][3] |
| channel 1 | [0][1][0] | [0][1][1] | [0][1][2] | [0][1][3] |
| channel 2 | [0][2][0] | [0][2][1] | [0][2][2] | [0][2][3] |
| adc[1] (board 1) | s0 | s1 | s2 | s3 |
|---|---|---|---|---|
| channel 0 | [1][0][0] | [1][0][1] | [1][0][2] | [1][0][3] |
| channel 1 | [1][1][0] | [1][1][1] | [1][1][2] | [1][1][3] |
| channel 2 | [1][2][0] | [1][2][1] | [1][2][2] | [1][2][3] |
The total number of elements is the product of the sizes: 2 x 3 x 4 = 24. Each valid index runs from zero up to one less than its size, so the first element is adc[0][0][0] and the last is adc[1][2][3].
| Declaration | Meaning | Total elements |
|---|---|---|
| int a[5] | 1D: a line | 5 |
| int a[3][4] | 2D: 3 rows of 4 | 12 |
| int a[2][3][4] | 3D: 2 tables of 3 x 4 | 24 |
| int a[2][2][2][2] | 4D: 2 blocks of 3D 2x2x2 | 16 |
How 3D Arrays in C Sit in Memory
Memory is a single line of bytes, so C must flatten the stack. It continues the row-major rule: the last index changes fastest. All samples of channel 0 come first, then channel 1, then channel 2, and only after the whole of board 0 does board 1 begin.
Memory order of adc[2][3][4] (offset = board*12 + channel*4 + sample)
The generalized formula for an array declared as a[D1][D2][D3] is:
offset = i * (D2 * D3) + j * D3 + k /* for a[i][j][k] */
Notice that D1 never appears, exactly like the number of rows in the 2D formula. That is why, in a declaration with an initializer list, only the first dimension may be left blank, and every other dimension must be written.
Declaring and Initializing a 3D Array
Initialization uses one more level of braces. Missing values become zero just as in two dimensions:
int cube[2][2][3] = {
{ /* table 0 */
{1, 2, 3},
{4, 5, 6}
},
{ /* table 1 */
{7}, /* rest of this row is 0 */
{8, 9} /* rest of this row is 0 */
}
};
For large sparse arrays the zero initializer = {0} or C99 designated initializers such as [1][0][2] = 55 are shorter and clearer. Reading and writing is the same as always: cube[1][1][1] is 9 here.
Program: Sizes, Layout and Access
This program fills the multi-board array with a running counter, then prints its size, one element, and the same element reached through a flat offset. Building it proves the formula above on your own machine.
/* arr3d.c - 3D array sizes, access and flat offset */
#include <stdio.h>
int main(void)
{
/* 2 sensor boards, 3 channels each, 4 samples per channel */
int adc[2][3][4];
int n = 0;
for (int b = 0; b < 2; b++)
for (int ch = 0; ch < 3; ch++)
for (int s = 0; s < 4; s++)
adc[b][ch][s] = n++;
printf("total elements : %zu\n", sizeof(adc) / sizeof(int));
printf("bytes : %zu\n", sizeof(adc));
printf("adc[1][2][3] = %d\n", adc[1][2][3]);
/* Same element through a flat view: offset = b*(3*4) + ch*4 + s */
int *flat = &adc[0][0][0];
int offset = 1 * (3 * 4) + 2 * 4 + 3;
printf("flat[%d] = %d\n", offset, flat[offset]);
printf("board 1, channel 0:");
for (int s = 0; s < 4; s++)
printf(" %d", adc[1][0][s]);
printf("\n");
return 0;
}
$ gcc -std=c11 -Wall -Wextra -o arr3d arr3d.c
$ ./arr3d
total elements : 24
bytes : 96
adc[1][2][3] = 23
flat[23] = 23
board 1, channel 0: 12 13 14 15
The byte count of 96 assumes the common case of a 4-byte int (24 x 4). On a 16-bit MCU where int is 2 bytes the result would be 48, which is a good reminder to always use sizeof and never hard-code sizes. The counter value stored at adc[1][2][3] is 23, the last position, and board 1, channel 0 holds 12 to 15 because that row starts at offset 12.
int adc_flat[24]; and compute the index yourself with the formula.
Real-World Uses in Embedded C Programming
| Use case | Index meaning | Example |
|---|---|---|
| Multi-sensor logging | [board][channel][sample] | adc[2][3][4] |
| RGB image block | [row][column][color] | pix[8][8][3] |
| Thermal camera frames | [frame][row][column] | frames[4][8][8] |
| Calibration tables | [sensor][temperature step][gain step] | cal[2][5][5] |
| Tic-tac-toe or game boards in stages | [move][row][column] | history[9][3][3] |
A good rule: if you find yourself writing three-level index expressions you cannot explain in plain words, create a struct or give the dimensions names. A 3D array shines when all three dimensions are genuinely uniform and the data is processed in regular loops.
Memory and Performance Considerations
- Size grows fast: a [8][8][3] array of 32-bit ints is already 768 bytes. On a microcontroller with a few kilobytes of RAM, check the product of the dimensions before declaring.
- Stack versus static: big local arrays live on the stack and can overflow it without warning. Use static or global storage for large buffers.
- Access order: keep the last index in the innermost loop so memory is read in order.
- Smaller element types: if samples are 12-bit ADC values, uint16_t halves the memory compared with a 32-bit int.
Common Mistakes and Troubleshooting
| Mistake | Symptom | Fix |
|---|---|---|
| Using index equal to the size, for example adc[2][0][0] | Reads past the array; random data or a crash | Valid indices end at size minus one |
| Mixing up index order | Wrong channel or sample | Write the meaning of each index in a comment |
| Omitting a middle dimension in the declaration | Compile error | Only the first dimension may be blank |
| Large local 3D array on a small stack | Random resets or hard faults | Make it static or global |
| Hard-coding sizeof as 4 bytes per int | Wrong on 16-bit targets | Always use sizeof(adc) |
Best Practices
- Use #define or enum names for each dimension, for example BOARDS, CHANNELS and SAMPLES.
- Keep the innermost loop on the last index.
- Use fixed-width types from stdint.h for data that comes from hardware.
- Print or inspect one known element, as we did with adc[1][2][3], to confirm your index order.
- Consider a struct array when each element has more than one field; do not stretch arrays beyond three levels.
Summary and Key Takeaways
- A 3D array is an array of 2D arrays: a[D1][D2][D3] holds D1 x D2 x D3 elements.
- The last index varies fastest in memory (row-major generalization).
- offset = i*(D2*D3) + j*D3 + k; the first dimension is not needed to find an element.
- Only the first dimension may be omitted when an initializer list is present.
- Use sizeof for counts and bytes; watch stack and RAM limits in embedded C programming.
Conclusion
You have now met arrays in one, two and three dimensions, and in each case the lesson was the same: the compiler turns your indices into one offset using the sizes of every dimension except the first. If you can explain that sentence, you understand arrays in C better than many working programmers. The next and final lecture of this chapter in the free embedded C course is a quiz and an interview question bank to test everything from this page range.
Frequently Asked Questions
How many elements are in int a[2][3][4]?
There are 2 x 3 x 4 = 24 elements.
Which index changes fastest in memory for a 3D array in C?
The last one. Elements with consecutive values of the last index are neighbours in memory.
Can I leave out more than the first dimension of a 3D array?
No. Only the first dimension may be left blank when you give an initializer list; all others are required.
How do I find the size of a 3D array in bytes?
Use sizeof(array). Dividing by sizeof of one element gives the total element count.
Is a 3D array the same as a pointer to a pointer to a pointer?
No. A true 3D array is one contiguous block, while a triple pointer refers to separately allocated pieces.
When should I use a struct instead of a 3D array?
When the elements have different types or meanings. Use arrays for uniform data processed with regular loops.
Test Yourself: Final Array Quiz
The next lecture is a quiz and interview question bank for this whole page range. Keep going with the free C programming course on EmbeddedPathashala.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments