Open In App

Initialization of Multidimensional Array in C

Last Updated : 18 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In C, multidimensional arrays are the arrays that contain more than one dimensions. These arrays are useful when we need to store data in a table or matrix-like structure. In this article, we will learn the different methods to initialize a multidimensional array in C.

The easiest method for initializing multidimensional arrays is by using the initializer list. We explicitly provide values for every element of the multidimensional array at the time of its declaration. Let’s take a look at an example:

C
#include <stdio.h>

int main() {
  
    // Initializing a 2x2 2D array
    int twoDarr[2][2] = { {1, 2}, {3, 4} };
  
      int threeDarr[2][2][2] = { {{1, 2}, {3, 4}},
                              {{5, 6}, {7, 8}} };

    // Printing 2D array
    printf("2D Array\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++)
            printf("%d ", twoDarr[i][j]);
        printf("\n");
    }
    printf("\n");
  
      // Printing 3D array
    printf("3D Array\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
              for (int k = 0; k < 2; k++)
                printf("%d ", threeDarr[i][j][k]);
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

Output
2D Array
1 2 
3 4 

3D Array
1 2 
3 4 

5 6 
7 8 

Explanation: In this example, we declare a 2×2 2D array a 2x2x2 3D array. We initialize it with values for each element. We can see that the values are nested in braces {} according to the number of rows and columns. The values from the list is assigned to the array elements sequentially starting from the leftmost one. The nesting of braces can be skipped but the sequence of assignment cannot be changed.

C also provides some other different methods to initialize the 2D array.

Partial Initialization

In partial initialization, you can initialize some values of the array, and the compiler will automatically fill the remaining elements with zero.

C
#include <stdio.h>

int main() {
    
    // Initializing only first row of a 2x3 array
      // First row initialized, second row gets 0s
    int arr[2][3] = { {1, 2, 3}  };

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++)
            printf("%d ", arr[i][j]);
        printf("\n");
    }

    return 0;
}

Output
1 2 3 
0 0 0 

Explanation: In this example, only the first row is initialized. Since the second row is not provided, the compiler automatically assigns 0 to all the elements of the second row.

Initialization to Zero

Just like 1D array, multidimensional arrays can be initialized to 0 by providing only single 0 in initializer list at the time of declaration.

C
#include <stdio.h>

int main() {
  
    // Declaring a 3x3 array without initializing it
    // The array is initialized to zero
    int arr[3][3] = {0};

      // Printing
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++)
            printf("%d ", arr[i][j]);
        printf("\n");
    }

    return 0;
}

Output
0 0 0 
0 0 0 
0 0 0 

Explanation: In this example, we explicitly set the first element {0}. The compiler fills the entire array with zero values as a result. This is helpful when you want to initialize all elements to zero without explicitly listing each one.

Ony by One Initialization

It is the last method of initialization where we initialize each element in the array individually by accessing the element and assigning it the initial value.

C++
#include <stdio.h>

int main() {
  
    // Declaring a 3x3 array without initializing it
    // The array is initialized to zero
    int arr[3][3];
  
      // Initializing it using continious values
      int c = 1;
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++)
            arr[i][j] = c++;
    }

      // Printing
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++)
            printf("%d ", arr[i][j]);
        printf("\n");
    }

    return 0;
}

Output
1 2 3 
4 5 6 
7 8 9 

Similarly 3D arrays can be initialized using 3 nested loops.

C++
#include <stdio.h>

int main()
{

    // Declaring a 2x2x2 array without initializing it
    int arr[2][2][2];

    // Initializing it using continious values
    int c = 1;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++)
                arr[i][j][k] = c++;
        }
    }

    // Printing
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++)
                printf("%d ", arr[i][j][k]);
            printf("\n");
        }
        printf("\n");
    }

    return 0;
}

Output
1 2 
3 4 

5 6 
7 8 


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg