- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Multi Dimensional Arrays
![]() Share with a Friend |
C Programming - C Multi Dimensional Arrays
C Multidimensional Arrays
Multidimensional arrays in C are arrays that contain more than one index or dimension. These arrays are used to store data in a tabular form (like matrices or grids) and allow us to access elements in more than one dimension.
In C, multidimensional arrays are generally declared and used for storing and managing data in rows and columns or higher dimensions.
- Declaration of Multidimensional Arrays
To declare a multidimensional array, we specify the number of elements in each dimension.
Syntax:
C
data_type array_name[size1][size2]...[sizeN];
Where:
- data_type is the type of data to store (e.g., int, float, etc.).
- array_name is the name of the array.
- size1, size2, ..., sizeN are the sizes of each dimension (the number of elements in each dimension).
- Two-Dimensional Arrays
A two-dimensional (2D) array is essentially an array of arrays. It can be visualized as a table with rows and columns.
Syntax for 2D Array:
C
data_type array_name[row_size][column_size];
For example:
C
int arr[3][4]; // A 2D array with 3 rows and 4 columns
This declares a 2D array arr with 3 rows and 4 columns. The memory for this 2D array is allocated contiguously, meaning that all 12 elements will be stored in a single block of memory.
- Initializing Two-Dimensional Arrays
You can initialize a 2D array at the time of declaration using nested braces {}.
Example:
C
int arr[3][4] = {
{1, 2, 3, 4}, // First row
{5, 6, 7, 8}, // Second row
{9, 10, 11, 12} // Third row
};
Alternatively, you can also partially initialize the array, and the remaining elements will be set to 0 by default (for global or static arrays).
Partial Initialization:
C
int arr[3][4] = {
{1, 2}, // First row
{5, 6}, // Second row
{9} // Third row (rest elements are initialized to 0)
};
- Accessing Elements in a Two-Dimensional Array
To access elements in a 2D array, you use two indices: one for the row and one for the column.
Syntax:
C
array_name[row_index][column_index];
Example:
C
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
printf("%d", arr[1][2]); // Outputs 7 (Second row, third column)
- Three-Dimensional Arrays
A three-dimensional (3D) array can be thought of as an array of 2D arrays, or a table with depth, rows, and columns. It requires three indices to access each element.
Syntax for 3D Array:
C
data_type array_name[size1][size2][size3];
For example:
C
int arr[2][3][4]; // A 3D array with 2 blocks, 3 rows, and 4 columns
- Initializing Three-Dimensional Arrays
You can initialize a 3D array using nested braces for each dimension.
Example:
C
int arr[2][3][4] = {
{
{1, 2, 3, 4}, // Block 1, Row 1
{5, 6, 7, 8}, // Block 1, Row 2
{9, 10, 11, 12} // Block 1, Row 3
},
{
{13, 14, 15, 16}, // Block 2, Row 1
{17, 18, 19, 20}, // Block 2, Row 2
{21, 22, 23, 24} // Block 2, Row 3
}
};
- Accessing Elements in a Three-Dimensional Array
To access an element in a 3D array, you need to use three indices: one for the block, one for the row, and one for the column.
Syntax:
C
array_name[block_index][row_index][column_index];
Example:
C
int arr[2][3][4] = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{
{13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}
}
};
printf("%d", arr[1][2][3]); // Outputs 24 (Block 2, Row 3, Column 4)
- Multi-Dimensional Arrays with Dynamic Sizes
If you want to dynamically allocate memory for a multidimensional array (for example, based on user input), you can use pointers and memory allocation functions like malloc() or calloc().
Example of Dynamic Allocation for 2D Array:
C
int rows = 3, cols = 4;
int **arr;
arr = (int **)malloc(rows * sizeof(int *)); // Allocate memory for rows
for(int i = 0; i < rows; i++) {
arr[i] = (int *)malloc(cols * sizeof(int)); // Allocate memory for columns
}
// Initialize and access elements
arr[0][0] = 1;
arr[1][2] = 5;
printf("%d", arr[1][2]); // Outputs 5
// Free allocated memory
for(int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
- Multidimensional Arrays and Pointers
In C, multidimensional arrays are stored in row-major order (in memory), meaning that elements of the first row are stored contiguously, followed by the second row, and so on.
Because arrays are closely related to pointers, you can use pointer arithmetic to access multidimensional array elements.
Example:
C
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
int *ptr = (int *)arr; // Treat the 2D array as a 1D array
printf("%d", *(ptr + 4)); // Access arr[1][1], outputs 5
- Practical Use Cases for Multidimensional Arrays
- Matrices: A 2D array is often used to represent matrices in mathematical computations (e.g., matrix multiplication, transformations).
- Game Boards: In games like chess or tic-tac-toe, a 2D array is a convenient way to represent the game board.
- Tables and Grids: Used in scenarios where data is structured in rows and columns (like spreadsheets).
Summary of Multidimensional Arrays in C
- Declaration: Arrays with more than one dimension (2D, 3D, etc.) are declared by specifying the size of each dimension.
- Initialization: Multidimensional arrays can be initialized using nested braces {}.
- Accessing Elements: You can access elements using multiple indices for each dimension.
- Memory Layout: Arrays are stored in contiguous memory locations, which makes accessing elements efficient.
- Pointer Arithmetic: The array name can be treated as a pointer, and pointer arithmetic can be used for accessing elements.
- Dynamic Memory Allocation: Multidimensional arrays can be dynamically allocated using pointers and memory allocation functions.
Multidimensional arrays in C are a powerful feature for organizing and managing large datasets, especially in fields like scientific computing, image processing, and simulations.
