
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Initialization of a multidimensional array in C
Array is a collection of same type of elements at contiguous memory location. The lowest address corresponds to the first element while highest corresponds to last element. Array index starts with zero(0) and ends with the size of array minus one(array size - 1). Array size must be integer greater than zero.
Let us see an example,
If array size = 10 First index of array = 0 Last index of array = array size - 1 = 10-1 = 9
Multi-dimensional arrays are arrays of array. The data is stored in tabular form in row major order.
The following is the syntax of multi-dimensional arrays.
type array_name[array_size1][array_size2].......[array_sizeN];
Here,
array_name − Any name given to an array.
array_size − Size of array.
The following is how you can initialize a multi-dimensional array.
type array_name[array_size1][array_size2].......[array_sizeN]; = { {elements} , {elements} , ... , {elements} }
The following is an example of multi-dimensional array.
Example
#include <stdio.h> int main () { int arr[2][3] = { {5,2,3}, {28,8,30}}; int i, j; for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 3; j++ ) printf("arr[%d][%d] = %d
", i, j, arr[i][j] ); } return 0; }
Output
arr[0][0] = 5 arr[0][1] = 2 arr[0][2] = 3 arr[1][0] = 28 arr[1][1] = 8 arr[1][2] = 30
In the above program, a two-dimensional array is declared.
int arr[2][3] = { {5,2,3}, {28,8,30}};
The elements of array are printed using nested for loop.
for ( i = 0; i < 2; i++ ) { for ( j = 0; j < 3; j++ ) printf("arr[%d][%d] = %d
", i, j, arr[i][j] ); }
- Related Articles
- Initialization of a multidimensional arrays in C/C++
- Initialization of a multidimensional arrays in C/C++ Program
- What is a multidimensional array in C language?
- Initialization of a normal array with one default value in C++
- How to print dimensions of multidimensional array in C++
- Multidimensional array in Java
- PHP Multidimensional Array.
- What is a Multidimensional array in Java?
- Is there a difference between copy initialization and direct initialization in C++?
- Initialization of static variables in C
- Variable initialization in C++
- Zero Initialization in C++
- Uniform Initialization in C++
- Collection Initialization in C#
- MongoDB multidimensional array projection?
