×

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

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels"...

4 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.