C Multidimensional Arrays

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Multidimensional Arrays?

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) */

Cheat Sheet

Keep these facts handy while you work with Multidimensional Arrays in C.

TopicDetail
Declaretype a[d1][d2][d3];
Fastest indexThe last one
Elementsd1 * d2 * d3
Function parameterGive all but the first size

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;
}

How It Works

  1. The innermost index k varies fastest, so values 0..7 fill in order.
  2. cube[1][0][1] is plane 1 (4 elements in), row 0, column 1: 4 + 0 + 1 = 5.
  3. 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.

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.

Next: Variable Length Arrays

C99 arrays whose size is decided at run time.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful