How to Get Value of Multidimensional Array in C?
Prerequisite: Array in C
An array is a type of data structure where we can store multiple elements of similar data types. A multidimensional array can be termed an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order.
Declaration of Array
Declaration or Initialisation of an array is where we declare the size, data_type, and name of an array.
Syntax:
data_type name_of_the_array [size 1][size 2]....[size N];
Here,
data_type : data type of elements to be inserted, like int , float ,etc.
size1 : size of 1D array
size2 : size of 2D array
….. Similarly, sizeN for N dimensional array.
Example with 2D and 3D array as defined below:
2-dimensional array
A two-dimensional array is an array where we can insert an array in place of elements.
char two[m][n]; //store upto m*n number of characters
3-dimensional array
int three[p][q][r]; //store upto p*q*r number of integers
Getting Value For a Multi-Dimensional Array
Elements in two-dimensional arrays are commonly referred to by x[i][j] where i is the row number and ‘j’ is the column number.
Example:
C
#include <stdio.h>
int main()
{
int arr1[3][4] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
int arr2[3][4]
= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
printf ( "Printing the value of the array 1 \n" );
for ( int i = 0; i < 3; i++) {
for ( int j = 0; j < 4; j++) {
printf ( "%d " , arr1[i][j]);
}
printf ( "\n" );
}
printf ( "----------\n" );
printf ( "Printing the value of the array 2 \n" );
for ( int i = 0; i < 3; i++) {
for ( int j = 0; j < 4; j++) {
printf ( "%d " , arr2[i][j]);
}
printf ( "\n" );
}
}
|
Javascript
const arr1 = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]];
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
console.log( "Printing the value of the array 1" );
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 4; j++) {
console.log(arr1[i][j]);
}
console.log( " " );
}
console.log( "----------" );
console.log( "Printing the value of the array 2" );
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 4; j++) {
console.log(arr2[i * 4 + j]);
}
console.log( " " );
}
|
Output
Printing the value of the array 1
1 2 3 4
5 6 7 8
9 10 11 12
----------
Printing the value of the array 2
1 2 3 4
5 6 7 8
9 10 11 12
Brackets are not mandatory for every dimension compiler allocates a consecutive block of memory. We will need one more dimension in order to declare a 3d array and we will need one more nested loop in order to print the values
Example:
C
#include<stdio.h>
int main(){
int arr1[2][2][3] = { { {1,2,3} , {4,5,6} }, { {7,8,9} , {10,11,12} } };
int arr2[2][2][3] = { 1,2,3,4 , 5,6,7,8 , 9,10,11,12 };
printf ( "Printing the value of the array 1 \n" );
for ( int i=0; i< 2 ; i++){
for ( int j = 0; j < 2; j++){
for ( int k=0; k < 3; k++)
printf ( "%d " , arr1[i][j][k]);
printf ( "\n" );
}
}
printf ( "----------\n" );
printf ( "Printing the value of the array 2 \n" );
for ( int i=0; i< 2 ; i++){
for ( int j = 0; j < 2; j++){
for ( int k=0; k < 3; k++)
printf ( "%d " , arr2[i][j][k]);
printf ( "\n" );
}
}
}
|
Output
Printing the value of the array 1
1 2 3
4 5 6
7 8 9
10 11 12
----------
Printing the value of the array 2
1 2 3
4 5 6
7 8 9
10 11 12
Using Pointers
We can create a multidimensional array of pointers. Pointers are one of the most useful features where we can point to some other address. So, it is quite beneficial to use an array of pointers in conditions where we want to store more than one address.
Example:
int *arr[5][5]; //creating a 2D integer pointer array of 5 rows and 5 columns.
int *arr[5][5][5]; //creating a 3D integer pointer array of 5 * 5 * 5 dimensions
Example 1:
C
#include<stdio.h>
int main(){
int arr1[3][4] = { {1,2,3,4} , {5,6,7,8} , {9,10,11,12} };
int (*arr2)[4] = arr1;
int *arr3[3][4];
printf ( "Printing the value of the array 2 \n" );
for ( int i=0; i< 3 ; i++){
for ( int j = 0; j < 4; j++){
printf ( "%d " , *(*(arr2 + i) + j));
arr3[i][j ] = &arr1[i][j];
}
printf ( "\n" );
}
printf ( "Printing the value of the array 3 \n" );
for ( int i=0; i< 3 ; i++){
for ( int j = 0; j < 4; j++){
printf ( "%d " , *arr3[i][j]);
}
printf ( "\n" );
}
}
|
Output
Printing the value of the array 2
1 2 3 4
5 6 7 8
9 10 11 12
Printing the value of the array 3
1 2 3 4
5 6 7 8
9 10 11 12
Example 2:
C
#include <stdio.h>
int main()
{
int arr1[2][2][3] = { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
int (*arr2)[2][3] = arr1;
int * arr3[2][2][3];
printf ( "Printing the value of the array 2 \n" );
for ( int i = 0; i < 2; i++) {
for ( int j = 0; j < 2; j++) {
for ( int k = 0; k < 3; k++) {
printf ( "%d\t" , *(*(*(arr2 + i) + j) + k));
arr3[i][j][k] = &arr1[i][j][k];
}
printf ( "\n" );
}
printf ( "\n" );
}
printf ( "\n" );
printf ( "Printing the value of the array 3 \n" );
for ( int i = 0; i < 2; i++) {
for ( int j = 0; j < 2; j++) {
for ( int k = 0; k < 3; k++) {
printf ( "%d\t" , *arr3[i][j][k]);
}
printf ( "\n" );
}
printf ( "\n" );
}
}
|
Output
Printing the value of the array 2
1 2 3
4 5 6
7 8 9
10 11 12
Printing the value of the array 3
1 2 3
4 5 6
7 8 9
10 11 12
Dynamic Memory Allocation
A two-dimensional array of pointers can also be created using Dynamic Memory Allocation. Dynamic Memory Allocation means that the memory allocated to the array or any other element will be heap memory rather than stack memory. We can use the malloc() function to dynamically allocate memory. To know more about Dynamic memory refer to Dynamic Memory Allocation.
Declaration:
ptr = (cast-type*) malloc(byte-size)
Example 1:
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr1[3][4] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
int *** arr2 = malloc (3 * sizeof ( int **));
for ( int i = 0; i < 3; i++) {
arr2[i] = malloc (4 * sizeof ( int *));
}
for ( int i = 0; i < 3; i++) {
for ( int j = 0; j < 4; j++) {
arr2[i][j] = &arr1[i][j];
}
}
printf ( "printing the value to the array 2 \n" );
for ( int i = 0; i < 3; i++) {
for ( int j = 0; j < 4; j++) {
printf ( "%d\t" , *arr2[i][j]);
}
printf ( "\n" );
}
free (arr2);
}
|
Output
printing the value to the array 2
1 2 3 4
5 6 7 8
9 10 11 12
Example 2:
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr1[2][2][3] = { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
int **** arr2 = malloc (2 * sizeof ( int ***));
for ( int i = 0; i < 2; i++) {
arr2[i] = malloc (2 * sizeof ( int **));
for ( int j = 0; j < 2; j++) {
arr2[i][j] = malloc (3 * sizeof ( int *));
}
}
for ( int i = 0; i < 2; i++) {
for ( int j = 0; j < 2; j++) {
for ( int k = 0; k < 3; k++) {
arr2[i][j][k] = &arr1[i][j][k];
}
}
}
printf ( "printing the value to the array 2 \n" );
for ( int i = 0; i < 2; i++) {
for ( int j = 0; j < 2; j++) {
for ( int k = 0; k < 3; k++) {
printf ( "%d\t" , *arr2[i][j][k]);
}
printf ( "\n" );
}
printf ( "\n" );
}
free (arr2);
}
|
Output
printing the value to the array 2
1 2 3
4 5 6
7 8 9
10 11 12
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...