DEV Community

Sujith V S
Sujith V S

Posted on

2 1 1 1 1

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.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more