Utilizor
Contact Us
HomeC TutorialC Multi-Dimensional Arrays

CC Multi-Dimensional Arrays

Arrays of arrays.

C Multi-Dimensional Arrays

A multidimensional array is basically an array of arrays.

Arrays can have any number of dimensions. The most common are two-dimensional arrays (2D).

int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };

The first dimension represents the number of rows [2], while the second dimension represents the number of columns [3].

Examples

Access 2D Array

c example

Accessing element at row 0, column 2.

#include <stdio.h>

int main() {
  int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
  printf("%d", matrix[0][2]);  // Outputs 2
  return 0;
}

Loop Through 2D Array

c example

Nested loop to access all elements.

#include <stdio.h>

int main() {
  int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
  int i, j;
  
  for (i = 0; i < 2; i++) {
    for (j = 0; j < 3; j++) {
      printf("%d\n", matrix[i][j]);
    }
  }
  return 0;
}

Test Your Knowledge

1. How many indexes do you need to access an element in a 2D array?

2. int arr[3][4] declares an array with...