Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Initialization of Multidimensional Array in C
Next article icon

Multidimensional Arrays in C – 2D and 3D Arrays

Last Updated : 07 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

A multi-dimensional array in C can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays include 2D arrays which grows in two dimensions, and 3D arrays which grows in three dimensions.

Syntax

The general form of declaring N-dimensional arrays is shown below:

C
type arrName[size1][size2]....[sizeN]; 
  • type: Type of data to be stored in the array.
  • arrName: Name assigned to the array.
  • size1, size2,…, sizeN: Size of each dimension.

Examples:

C
// Two-dimensional array int two_d[10][20];  // Three-dimensional array: int three_d[10][20][30]; 

Size of Multidimensional Arrays

The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of both dimensions. Consider the array arr[10][20]:

  • The array int arr[10][20] can store total of (10*20) = 200 elements.

To get the size in bytes, we multiply the size of a single element (in bytes) by the total number of elements in the array.

  • The size of array int arr[10][20] = 10 * 20 * 4  = 800 bytes, where the size of int is 4 bytes.

Types of Multidimensional Arrays

In C, there can be many types of arrays depending on their dimensions but two of them are most commonly used:

  1. 2D Array – Two Dimensional
  2. 3D Array – Three Dimensional

2D Arrays in C

A two-dimensional array or 2D array is the simplest form of the multidimensional array. We can visualize a two-dimensional array as one-dimensional arrays stacked vertically forming a table with ‘m’ rows and ‘n’ columns. In C, arrays are 0-indexed, so the row number ranges from 0 to (m-1) and the column number ranges from 0 to (n-1).

2d-array-in-c

Declaration of 2D Array

A 2D array with m rows and n columns can be created as:

C
type arr_name[m][n]; 

For example, we can declare a two-dimensional integer array with name ‘arr’ with 10 rows and 20 columns as:

C
int arr[10][20]; 

Initialization of 2D Arrays

We can initialize a 2D array by using a list of values enclosed inside ‘{ }’ and separated by a comma as shown in the example below:

C
int arr[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}; 

or

C
int arr[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}}; 

The elements will be stored in the array from left to right and top to bottom. So, the first 4 elements from the left will be filled in the first row, the next 4 elements in the second row, and so on. This is clearly shown in the second syntax where each set of inner braces represents one row.

In list initialization, we can skip specifying the size of the row. The compiler will automatically deduce it in this case. So, the below declaration is valid.

C
type arr_name[][n] = {...values...}; 

It is still compulsory to define the number of columns.

Note: The number of elements in initializer list should always be either less than or equal to the total number of elements in the array.

Accessing Elements

An element in two-dimensional array is accessed using row indexes and column indexes inside array subscript operator [].

C
arr_name[i][j] 

2D Array Traversal

Traversal means accessing all the elements of the array one by one. We will use two loops, outer loop to go over each row from top to bottom and the inner loop is used to access each element in the current row from left to right.

C
for(int i = 0; i < row; i++){     for(int j = 0; J < col; j++){         arr[i][j];     } } 

The below example demonstrates the row-by-row traversal of a 2D array.

C
#include <stdio.h>  int main() {        // Create and initialize an array with 3 rows   	// and 2 columns     int arr[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };      // Print each array element's value     for (int i = 0; i < 3; i++) {         for (int j = 0; j < 2; j++) {             printf("arr[%d][%d]: %d    ", i, j, arr[i][j]);         }       	printf("\n");     }     return 0; } 

Output
arr[0][0]: 0    arr[0][1]: 1     arr[1][0]: 2    arr[1][1]: 3     arr[2][0]: 4    arr[2][1]: 5     

How 2D Arrays are Stored in the Memory?

As an array, the elements of the 2D array have to be stored contiguously in memory. As the computers have linear memory addresses, the 2-D arrays must be linearized so as to enable their storage. There are two ways to achieve this:

  • Row Major Order: This technique stores the 2D array row after row. It means that the first row is stored first in the memory, then the second row of the array, then the third row, and so on.
  • Column Major Column: This technique stores the 2D array column after column. It means that first column is stored first in the memory, then the second column, then the third column, and so on.

This allows the random access to the 2D array elements as the computer need not keep track of the addresses of all the elements of the array. It only tracks of the Base Address (starting address of the very first element) and calculates the addresses of other array elements using the base address.

Passing 2D Arrays to Functions

Passing 2D arrays to functions need a specialized syntax so that the function knows that the data being passed is 2d array. The function signature that takes 2D array as argument is shown below:

3D Array in C

A Three-Dimensional Array or 3D array in C is a collection of two-dimensional arrays. It can be visualized as multiple 2D arrays stacked on top of each other.

3d-array-in-c

Declaration of 3D Array in C

We can declare a 3D array with x 2D arrays each having m rows and n columns using the syntax shown below:

C
type arr_name[x][m][n]; 

For example, we can declare 3d array, which is made by 2-2D array and each 2D array have 2 rows and 2 columns:

C
int arr[2][2][2]; 

Initialization of 3D Array in C

Initialization in a 3D array is the same as that of 2D arrays. The difference is as the number of dimensions increases so the number of nested braces will also increase.

C
int arr[2][3][2] = {0, 1, 2, 3, 4, 5, 6, 7 , 8, 9, 10, 11} 

or

C
int arr[2][3][2] = { { { 1, 1 }, { 2, 3 }, { 4, 5 } },                          { { 6, 7 }, { 8, 9 }, { 10, 11 } } }; 

Again, just like the 2D arrays, we can also declare the 3D arrays without specifying the size of the first dimensions if we are using initializer list. The compiler will automatically deduce the size of the first dimension. But we still need to specify the rest of the dimensions.

C
arr [][2][2] = {...Values...}; 

Accessing Elements

To access elements in 3D array, we use three indexes. One for depth, one for row and one for column inside subscript operator [].

C
arr_name[d][i][j] 

where, d, i and j are the indexes for depth (representing a specific 2D array.), the row within that 2D array, and the column within that 2D array respectively.

3D Array Traversal

To traverse the entire 3D array, you need to use three nested loops: an outer loop that goes through the depth (or the set of 2D arrays), a middle loop goes through the rows of each 2D array and at last an inner loop goes through each element of the current row.

C
for(int d = 0; d < depth; d++){     for(int i = 0; i < row; i++){         for(int j = 0; j < col; j++){             arr[d][i][j];         }     } } 

Let’s take a simple example to demonstrate all the concepts we discussed about 3D arrays:

C
#include <stdio.h>  int main() {        // Create and Initialize the      // 3-dimensional array     int arr[2][3][2] = { { { 1, 1 }, { 2, 3 },                         { 4, 5 } }, { { 6, 7 },                         { 8, 9 }, { 10, 11 } } };    	// Loop through the depth     for (int i = 0; i < 2; ++i) {              	// Loop through the        	// rows of each depth         for (int j = 0; j < 3; ++j) {                      	// Loop through the            	// columns of each row             for (int k = 0; k < 2; ++k)                 printf("arr[%i][%i][%i] = %d   ", i, j, k,                        arr[i][j][k]);           	printf("\n");         }       printf("\n\n");     }     return 0; } 

Output
arr[0][0][0] = 1   arr[0][0][1] = 1    arr[0][1][0] = 2   arr[0][1][1] = 3    arr[0][2][0] = 4   arr[0][2][1] = 5      arr[1][0][0] = 6   arr[1][0][1] = 7    arr[1][1][0] = 8   arr[1][1][1] = 9    arr[1][2][0] = 10   arr[1][2][1] = 11   

How 3D Arrays are Stored in the Memory?

Like 2D arrays, the elements of a 3D array should also be stored contiguously in memory.

3d-array-breakdown

3D Array

Since computers have linear memory, the 3D array must also be linearized for storage. We use the same two techniques, the Row Major Order and Column Major Order but with added dimension. The elements are first stored layer by layer (or 2D array by 2D array). Within each 2D array, the elements follow the corresponding row or column major order.

Passing 3D Arrays to Functions

Passing a 3D array to a function in C is similar to passing 2D arrays, but with an additional dimension. When passing a 3D array, you need to pass the sizes of all the dimensions separately because the size information of array is lost while passing.



Next Article
Initialization of Multidimensional Array in C
author
kartik
Improve
Article Tags :
  • C Language
  • C++
  • c-array
  • C-Arrays
  • cpp-array
Practice Tags :
  • CPP

Similar Reads

  • C Arrays
    An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., and also derived and user-defined data types such as pointers, structures, etc. Array DeclarationIn C,
    9 min read
  • Properties of Array in C
    An array in C is a fixed-size homogeneous collection of elements stored at a contiguous memory location. It is a derived data type in C that can store elements of different data types such as int, char, struct, etc. It is one of the most popular data types widely used by programmers to solve differe
    8 min read
  • Length of Array in C
    The Length of an array in C refers to the maximum number of elements that an array can hold. It must be specified at the time of declaration. It is also known as the size of an array that is used to determine the memory required to store all of its elements. In C language, we don't have any pre-defi
    3 min read
  • Multidimensional Arrays in C - 2D and 3D Arrays
    A multi-dimensional array in C can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays include 2D arrays which grows in two dimensions, and 3D arrays which grows in three dimension
    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
  • Jagged Array or Array of Arrays in C with Examples
    Prerequisite: Arrays in CJagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays. Example: arr[][] = { {0, 1, 2}, {6, 4}, {1, 7, 6, 8, 9}
    3 min read
  • Pass Array to Functions in C
    Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C. In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, wh
    3 min read
  • How to pass a 2D array as a parameter in C?
    A 2D array is essentially an array of arrays, where each element of the main array holds another array. In this article, we will see how to pass a 2D array to a function. The simplest and most common method to pass 2D array to a function is by specifying the parameter as 2D array with row size and c
    3 min read
  • How to pass an array by value in C ?
    In C programming, arrays are always passed as pointers to the function. There are no direct ways to pass the array by value. However, there is trick that allows you to simulate the passing of array by value by enclosing it inside a structure and then passing that structure by value. This will also p
    2 min read
  • Variable Length Arrays (VLAs) in C
    In C, variable length arrays (VLAs) are also known as runtime-sized or variable-sized arrays. The size of such arrays is defined at run-time. Variably modified types include variable-length arrays and pointers to variable-length arrays. Variably changed types must be declared at either block scope o
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences