×

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C

The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant order.

Syntax

The general form of a multidimensional array can be written as;

 datatype array_name [size 1] [size 2] [size 3] … [size N];
 Or
 datatype array_name [number of tables] [size_of_row][size of column]; 

Where;

  • Data type:It is the data type present in the C standard in which the array elements will be stores. Any data type in the C standard is applicable such as, int, float, double, and so on.
  • array_name: It is the name of the array which is user specified.
  • size 1, size 2, size 3,... size N: These are the size of the dimension which will represent an array.
  • Number of tables: It is the total number of tables an array can accept such as, a 2D array can consist of up to single table while other multidimensional might have more of it.
  • size_of_row: It represents the elements a row can store.
  • size_of_column: It represents the elements a column can store.

Consider a three-dimensional array, and it can be represented as;

int threeD [5][6][3];

A two-dimensional array is represented as;

int twoD [5][6];

The total elements that will be stored in a multidimensional array can be calculated by just multiplying the size of all the dimensions that the user will declare.

E.g.:

int threeD [5][6][3];

The above-mentioned array can store a total of (5 * 6 * 3) elements, i.e., 90 elements.

In the same manner, an array int a[10][20] can store a total of (10 * 20) elements, i.e., 200 elements.

Two-dimensional array

It is one of the simplest forms of the multidimensional array. A two-dimensional array or a 2D array is a list of a one-dimensional array. Hence to declare a 2D array integer array of size [m][n], the declaration would be something like; int twoDimensional [m][n]; .

A two-dimensional array can also be called as a matrix.There are two ways of initializing a two-dimensional array during the time of declaration.

 int twoDim [3][3] = {
             {2, 5, 6},
             {4, 1, 9},
             {3, 4, 8}
 }; 

Or

int twoDim[3][3] = { 2, 5, 6, 4, 1, 9, 3, 4, 8};

The above two declarations are the ones where the values are decided while writing the program itself. In order to make that user input, for loops can be used and then can be displayed and more operations on it can be performed.

E.g.:

 #include<stdio.h>
 int main()
 {
 /* 2D array declaration*/
 int disp[2][3];
 /*Counter variables for the loop*/
 int i, j;
 for(i=0; i<2; i++)
 {
 for(j=0;j<3;j++)
 {
 printf("Enter value for disp[%d][%d]:", i, j);
 scanf("%d", &disp[i][j]);
 }
 }
 //Displaying the array elements
 printf("Two Dimensional array elements:\n");
 for(i=0; i<2; i++)
 {
 for(j=0;j<3;j++)
 {
 printf("%d ", disp[i][j]);
 if(j==2)
 {
 printf("\n");
 }
 }
 }
 return 0;
 } 

Output

Multi-dimensional Arrays in C

Three-dimensional array

Initializing a three-dimensional array is as same as that of the two-dimensional array. Hence, the difference is that the number of dimensions increases so that the number of nested braces will also increase.

Syntax

int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

While accessing the elements in the 3D(three dimensional) array, which is mostly similar to the 2D array, one has to make use of the three loops instead of the two loops for one additional dimensional in the 3D array.

E.g.:

 // C Program to store and print 12 values entered by the user
 #include <stdio.h>
 int main()
 {
 int test[2][3][2];
 printf("Enter 12 values: \n");
 for (int i = 0; i< 2; ++i)
 {
 for (int j = 0; j < 3; ++j)
 {
 for (int k = 0; k < 2; ++k)
 {
 scanf("%d", &test[i][j][k]);
 }
 }
 }
   // Printing values with proper index.
 printf("\nDisplaying values:\n");
 for (int i = 0; i< 2; ++i)
 {
 for (int j = 0; j < 3; ++j)
 {
 for (int k = 0; k < 2; ++k)
 {
 printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
 }
 }
 }
 return 0;
 } 

Related Topics

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

Find occurrence of substring in C using function

Introduction: In this article, we discuss finding the occurrence of a substring in C using function. This software takes strings and substrings as input and counts the occurrences of substrings within...

3 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.