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
C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() In C, a variable defined in a function is stored in the stack memory. The requirement of this memory is that it needs to know the size of the data to memory at compile time (before the program runs). Also, once defined, we can neither change the size nor completely delete the memory.To resolve this,
9 min read
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read