English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
An array is a collection of elements of the same type in consecutive memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element. Array indices start at zero and end at array size minus one (array size-1)end. The array size must be a positive integer.
Let's look at 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 arrays. Data is stored in a tabular form in the main order of rows.
The following is the syntax of a multi-dimensional array.
type array_name[array_size1]= [array_size2]= [array_sizeN];
Here,
array_name-Name any array.
array_size-Size of the array.
The following is how to initialize a multi-dimensional array.
type array_name[array_size1]= [array_size2]= { {elements} , {elements} , ... , {elements} }
The following is an example of a multi-dimensional array.
#include <stdio.h> int main () { int arr[2]= [3]= { {5,2,3}, {28,8,30}}; int i, j; for ( i = 0; 2; i++ ) { for ( j = 0; 3; j++ ) printf("arr[%d][%d] = %d\n", i, j, arr[i][j]); } return 0; }
Output Result
arr[0][0] = 5 arr[0][1]= arr[ 2 arr[0][2]= arr[ 3 ][0] = arr[1][0] = arr[ 28 ][0] = arr[1]= [1]= arr[ 8 ][0] = arr[1]= [2]= arr[ 30
In the above program, a two-dimensional array is declared.
int arr[2]= [3]= { {5,2,3}, {28,8,30}};
The elements of the array are printed using nested for loops.
for ( i = 0; 2; i++ ) { for ( j = 0; 3; j++ ) printf("arr[%d][%d] = %d\n", i, j, arr[i][j]); }