DEV Community

Sujith V S
Sujith V S

Posted on

Multi-dimensional Array

Each element of multi dimensional array is an array. This is why it is called as array of arrays.

Syntax:
int arr[2][3];

int - datatype
arr - name of array.
2 - there will be two arrays inside this array.
3 - each array will have 3 elements.

int arr[2][3] = {{1,2,3}, {6,7,8}};

Access array elements

int arr[2][3] = {{1,2,3}, {6,7,8}}; printf("%d ", arr[0][0]); printf("%d ", arr[1][2]); 
Enter fullscreen mode Exit fullscreen mode

Here 1 is the second array and 2 is the third element in this array.

Change array values.

int main() { int arr[2][3] = {{1,2,3}, {6,7,8}}; arr[0][2] = 7; arr[1][1] = 8; printf("%d", arr[0][2]); printf("%d", arr[1][1]); return 0; } 
Enter fullscreen mode Exit fullscreen mode

Multidimensional array using for loop

#include <stdio.h> int main() { int arr[2][3] = {{1,2,3}, {6,7,8}}; for(int i=0; i<2; i++){ for(int j=0; j<3; j++){ printf("%d ", arr[i][j]); } } return 0; } 
Enter fullscreen mode Exit fullscreen mode

First for loop for each arrays inside and the second for loop is for the elements inside each array.

Top comments (0)