Skip to main content

Multidimensional arrays in C

Multidimensional array is also known as array of arrays.  C allows user to write arrays with one or more dimensions.  An array with more than one dimension is a multi-dimensional array.  Two dimensional array is a simplest form of multidimensional array.  Below is the general form of multidimensional array.

data_type  array_name[size1][size2][size3]..[sizeN];

Consider the following example
int arr[2][2][2] = {   { {1, 1}, {1, 1} },
                                  { {1, 1}, {1, 1} }
                              };
 
The above statement shows the declaration and initialization of multidimensional array(3d array).  Total number of elements in an array can be obtained by multiplying the size value in each dimension.

int arr[2][2][2];
2 X 2 X 2 = 8 elements in the array arr

And multidimensional array can be accessed using its array indices.
arr[0][0][1] = 2;

The above statement assigns 2 to arr[0][0][1].  Then the resultant value of the array arr would be the following.
{ 
     { {1, 2}, {1, 1} },
     { {1, 1}, {1, 1} }
};


Example C program using multidimensional arrays:
#include <stdio.h> 
int main() {
int i, j, k, three_dim[2][2][2] = { // array initialization
{{1, 1}, {1, 1}},
{{1, 1}, {1, 1}}
};

// printing elements of the array
printf("Elements of the array three_dim:\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
for (k = 0; k < 2; k++)
printf("three_dim[%d][%d][%d]: %d\n",
i, j, k, three_dim[i][j][k]);

printf("\nAssign 2 to three_dim[0][0][1]\n");
three_dim[0][0][1] = 2; // modifying an array element

// printing elements of the array
printf("\nElements of the array after modification:\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
for (k = 0; k < 2; k++)
printf("three_dim[%d][%d][%d]: %d\n",
i, j, k, three_dim[i][j][k]);
return 0;
}

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Elements of the array three_dim:
  three_dim[0][0][0]: 1
  three_dim[0][0][1]: 1
  three_dim[0][1][0]: 1
  three_dim[0][1][1]: 1
  three_dim[1][0][0]: 1
  three_dim[1][0][1]: 1
  three_dim[1][1][0]: 1
  three_dim[1][1][1]: 1

  Assign 2 to three_dim[0][0][1]

  Elements of the array after modification:
  three_dim[0][0][0]: 1
  three_dim[0][0][1]: 2
  three_dim[0][1][0]: 1
  three_dim[0][1][1]: 1
  three_dim[1][0][0]: 1
  three_dim[1][0][1]: 1
  three_dim[1][1][0]: 1
  three_dim[1][1][1]: 1



Comments

Popular posts from this blog

Array of pointer to structure

Array of pointer to structure is nothing but an array whose elements are pointers to structure How to declare array of pointer to structure? Below is an example declaration for array of pointer to structure. struct student *arr[10]; Here, arr is an array 10 of pointer to structure student. Dynamic memory allocation: Below is the procedure to perform dynamic memory allocation for the structure pointers in an array. struct student { int age; char name[32]; }; fun() { int i = 0; struct student *arr[10]; for (i = 0; i < 10; i++) { /* dynamic memory allocation for 5 structure objects */ arr[i] = (struct student *)malloc(sizeof(struct student) * 5); } } Write a c program to pass an array of structure pointers to another function   #include <stdio.h>   #include <stdlib.h>   #include <string.h>   struct student {         int age, rollno;         char name[32];   };   /* printing details of stude...

Nested function in C

What is nested function? If a function is defined inside another function, then it is called as nested function.  Nested function example in C: Consider the following example, int add(int a, int b) {        void print(int res) {          printf("Result is %d, res);       }       print(a+b);       return 0; } Here, print() is a nested function.  Because, it is defined inside another function named add() . Example C program using nested functions: #include <stdio.h> void add(int a, int b) { void print(int res) { // nested function printf("Result is %d\n", res); return; } print(a + b); // passing sum of a and b as parameter return; } int main() { add(10, 20); return 0; }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Result is 30 Previous Next

Difference between asterisk and ampersand operators in c

Address Operator (& - Ampersand): Returns address of the given variable.  Consider the following example, int *ptr, var = 10; ptr = &var; ptr = &var sets the address of the variable var to pointer ptr.  & is also called as reference operator. Let us try to understand the purpose of reference operator using the following example program.   #include <stdio.h>   int main() {         int var = 10, *ptr;         /* assigning address of variable var to pointer ptr */         ptr = &var;         /* printing the address of the variable var */         printf("Address of var is 0x%x\n", &var);         /* printing the value of pointer ptr */         printf("Value of ptr is 0x%x\n", ptr);         return 0;   }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Address of var is 0x...