How to Get Value of Multidimensional Array in C?
Last Updated :
27 Jan, 2023
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
// C program to Get value for
// a multi-dimensional array
#include <stdio.h>
// Driver code
int main()
{
// integers array with 3 rows and 4 columns
int arr1[3][4] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
// brackets is not mandatory except the edges
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
// integers array with 3 rows and 4 columns
const arr1 = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]];
// brackets is not mandatory except the edges
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(" ");
}
OutputPrinting 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
// C implementation for a three-dimensional array
#include<stdio.h>
int main(){
// integers array with 2 * 2 * 3 dimensions
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");
}
}
}
OutputPrinting 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
// implementation for 2d array
#include<stdio.h>
int main(){
// integers array with 3 rows and 4 columns
int arr1[3][4] = { {1,2,3,4} , {5,6,7,8} , {9,10,11,12} };
int (*arr2)[4] = arr1; // arr2 pointing to the address of 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]; // assigning the address to array 3
}
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");
}
}
OutputPrinting 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
// C implementation for 3d array
#include <stdio.h>
int main()
{
// integers array with 2 * 2 * 3 dimensions
int arr1[2][2][3] = { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
// arr2 pointing to the address of arr1
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));
// assigning the address to array 3
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");
}
}
OutputPrinting 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
// C Program to implement 2-D array
// using Dynamic Memory Allocation
#include <stdio.h>
#include <stdlib.h>
int main()
{
// integers array with 3 rows and 4 columns
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*));
}
// assigning the value to the array 2
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); // deallocating the memory
}
Outputprinting the value to the array 2
1 2 3 4
5 6 7 8
9 10 11 12
Example 2:
C
// C Program to implement 3-D array
// using Dynamic Memory Allocation
#include <stdio.h>
#include <stdlib.h>
int main()
{
// integers array with 2 * 2 * 3 dimensions
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*));
}
}
// assigning the value to the array 2
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); // deallocating the memory
}
Outputprinting the value to the array 2
1 2 3
4 5 6
7 8 9
10 11 12
Similar Reads
Multidimensional Arrays in Excel VBA
Multidimensional Arrays are used to store data of similar data types of more than one dimension. A multidimensional array has a dimension up to 60 but usually, we don't use arrays of dimensions more than 3 or 4. Here, we will see how to declare a multidimensional array in many ways and also how to c
2 min read
How to iterate a Multidimensional Array?
Multidimensional arrays are arrays that have more than one dimension. For example, a simple array is a 1-D array, a matrix is a 2-D array, and a cube or cuboid is a 3-D array but how to visualize arrays with more than 3 dimensions, and how to iterate over elements of these arrays? It is simple, just
8 min read
Multidimensional Array in R
Arrays are the R data objects which can store data in more than two dimensions. For example: If we create an array of dimensions (2, 3, 4) then it creates 4 rectangular matrices each with 2 rows and 3 columns. These types of arrays are called Multidimensional Arrays. Arrays can store only data types
3 min read
Get value from Multidimentional Array containing an Object
To get a value from a multidimensional array containing objects in C++, you can use the same syntax as you would for a regular multidimensional array, with the added step of accessing the appropriate member variable of the object. Simply use the appropriate index notation to access the object in the
6 min read
C++ Multidimensional Array
A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more.Syntax of Multidimensional ArraysC++data_type array_name[s1][s2]...[sn];where s1, s2,â¦, sn are the
8 min read
Initialization of Multidimensional Array in C
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 initial
4 min read
Initialization of Multidimensional Arrays in C++
In C++, multidimensional arrays are the type of arrays that have multiple dimensions, i.e., they can expand in multiple directions. In this article, we will discuss how to initialize the multidimensional arrays in C++. Methods to Initialize Multidimensional Array in C++We can initialize multidimensi
3 min read
Multidimensional Pointer Arithmetic in C/C++
In C/C++, arrays and pointers have similar semantics, except on type information. As an example, given a 3D array int buffer[5][7][6]; An element at location [2][1][2] can be accessed as "buffer[2][1][2]" or *( *( *(buffer + 2) + 1) + 2). Observe the following declaration T *p; // p is a pointer to
4 min read
Perl | Multidimensional Arrays
Multidimensional arrays in Perl are the arrays with more than one dimension. Technically there is no such thing as a multidimensional array in Perl but arrays are used to act as they have more than one dimension. Multi dimensional arrays are represented in the form of rows and columns, also knows as
6 min read
Find and return index of given String in a Multidimensional Array
Given a multidimensional array stringArr of strings and a keyString to be found, the task is to return the coordinates/indexes of that key string in the array. Example: Input: stringArr[][] = { { "a", "h", "b"}, {"c", "d", "e"}, {"g", "t", "r"} }, keyString = "e"Output = {1, 2}Explanation: Following
10 min read