• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Array » Multidimensional Arrays in C
Next →
← Prev

Multidimensional Arrays in C

By Dinesh Thakur

C language permits the use of multidimensional arrays. It is easy to visualize arrays up to three dimensions. However, we may have difficulty in visualizing a four, five or in general, an n-dimensional array.

A four-dimensional array can be thought of as a one-dimensional array in which each element is a three-dimensional array or as a matrix in which each element itself is a matrix or even as a three dimensional array having one-dimensional arrays as its elements.

Consider for example a school having six classes (5 to 10) each having up to three divisions (A, B and C). Each division can have up to 60 students and in any particular examination, each student has to appear for up to 10 subjects depending on his/her class. ·

Consider now that we wish to store the marks scored by students in a particular examination. The marks obtained by a student can be stored in a vector of size 10, whereas we require a matrix of size 60 x 10 to store the marks of all the students in any division and a three-dimensional array of size 3 x 60 x 10 to store the marks of all the students in a particular class. Now how can we store the marks of all the students in the school? By extending the above discussion, we can use a four-dimensional array of size 6 x 3 x 60 x 10. This array can now be considered as six arrays of size 3 x 60 x 10 or as a matrix of size 6 x 3 whose individual elements are matrices of size 60 x 10 or even as a three-dimensional array of size 6 x 3 x 60 where each element is a vector of size 10.

We can extend this discussion further and use a five-dimensional array to store the data of all the examination conducted by this school in an academic year and a six-dimensional array to store the data for several years.

We’ll be covering the following topics in this tutorial:

  • Declaration
  • Element Access and Operations on Elements and Entire Arrays
  • Initialization
  • Multidimensional Arrays as Function Parameters

Declaration

The declaration of an n-dimensional array takes the following form:

arr_type arr_name [size1] [size2] … [size_n] ;

where arr_ name is the name of the array being declared, each element of which is of type arr_type. The expressions size 1, size2, … , size_n enclosed in square brackets are integral constants or constant integral expressions that specify the number of elements in each dimension of the array.

As with vectors and matrices, we can declare multidimensional arrays of any built-in or user-defined type and the array elements are numbered starting from 0 (and not from 1) in each dimension.

Element Access and Operations on Elements and Entire Arrays

An element of a multidimensional array can be accessed by writing the array name followed by subscript expressions within subscript operators as shown below.

arr_name [exprl] [expr2] … [expr_n]

We can perform various operations on the elements of multidimensional arrays, similar to those discussed in one- and two-dimensional arrays. Thus, for an array of a built-in type, we can use an array element in an expression, assign a value to it, perform increment and decrement operations on it, pass it as an argument to a function and so on.

As with one- and two-dimensional arrays, C language does not provide any statement or operator to perform operations on the entire multidimensional array. Nested for loops are usually used to perform operations on an entire array as illustrated below for a three dimensional array a of size m x n x p:

for (i = 0; i < m; i++) {

for (j = 0; j < n; j++)

for (k = O; k < p; k++) {

/*process element a[i][j][k] */

    }

  }

}

Initialization

C language allows the initialization of multidimensional arrays using a syntax similar to that used for initialization of vectors and matrices. First, we can initialize them using a single comma separated list enclosed in braces as follows

data_type arr_name[size1] [size2] … [size_n] = {expr1, expr2, …};

As with vectors and matrices, the initialization list may be empty causing all the array elements to be initialized to default values which is 0 for arithmetic types.

We can also use a nested syntax similar to that used for the initialization of a matrix. For example, a three-dimensional array of size 2 x 3 x 4 can be initialized as shown below.

int a[2] [3] [4] = {

{

       {100, 101, 102, 103},

       {l10, 111 , 112, 113},

       {120, 121, 122, 123}

} ,

{

      {200, 201, 202, 203},

      {210, 211, 212, 213},

      {220, 221, 222, 223}

}

};

Note that we can omit the first array dimension (2) in the above initialization. Also, we can specify fewer elements for any row or fewer rows for any matrix. In such cases, the remaining elements in that row or matrix will be initialized to default values. Consider for example, the initialization statement given below.

int b[2] [3] [4] = {{{l, 2},{3, 4, 5)}};

This statement initializes array bas shown below.

                 Multidimensional Arrays in C

Multidimensional Arrays as Function Parameters

C language allows multidimensional arrays to be passed as actual arguments to a function. Most of the discussion about two-dimensional arrays in the previous section is applicable to multidimensional arrays as well. Thus, while declaring a multidimensional array as a function parameter, we may omit the first array dimension only. All the remaining array dimensions must be specified. In the calling functions, the size of the argument array must match these specified array dimensions.

Note that as with vectors and matrices, we can pass the actual size of the array to be processed using additional parameters to this function. Also, recall that all the arrays including multidimensional arrays are passed by reference.

Consider the function definition given below that accepts an integer array of size M x 10 x 10 and three parameters m, n and p that specify the actual size of the array to be processed.

void func(int arr[][10][10], int m, int n, int p)

{

/* process elements of array arr */

}

Some calls to this function are shown below.

int main()

{

      int a[10] [10] [10], b[5] [10] [10], c[50] [10] [10], d[10] [10] [5];

      func(a, 10, 10, 10); /* ok */

      func(b, 3, 4, 5); /* ok */

      func(c, 25, 10, 8); /* ok */

      func(d, 5, 5, 5);

      func(d, 10, 4, 5);

}

Illustrates initialization of multi-dimensional arrays

#include <stdio.h> 
  void main() 
  { 
     int i,j; 
     int San[3] [4] = {{1, 2, 3, 4}, {6, 7, 8, 9}, { 3, 5, 7, 9}}; 
     int Din [3] [4] = {{1, 2 },{6,7,8}, { 7}}; 
     //The above initialization contains fewer elements. 
     int Dal [3] [4] = {1,2,3,4,11,12,13,14,21}; 
     // The above initialization also has fewer elements. 
     clrscr(); 
     for(i=0;i<3; i++) 
     { 
       for (j =0; j<4; j++) 
          { 
               printf("\nSan[%d] [%d] = %d\t", i, j, San[i] [j]); 
               printf("Din[%d] [%d] = %d\t Dal[%d] [%d] = %d ", i, j, Din[i] [j], i,j, Dal [i] [j] ) ;  
             } 
               printf ("\n"); 
      } 
  } 

You’ll also like:

  1. Multidimensional Arrays and Pointers in C
  2. Multidimensional Array in Java Example
  3. Arrays Of Arrays with Varying Length in Java
  4. Characteristics of Arrays in C
  5. Three-Dimensional Arrays in C
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW