Declare C multidimensional array #
C multidimensional array has more than one subscript. To declare a multidimensional array, you use the following syntax:
type name[size][size2][size3]…
Code language: C++ (cpp)
Let’s examine the syntax above:
- First, you specify the data type for the elements of the multidimensional array. Of course, it can be any valid data type such as int, character, float, pointer, struct, etc.
- Second, you provide the name of the multidimensional array. The name should follow the naming rules of variables.
- Third, you determine the multidimensional array’s dimensions and their sizes. The number of dimensions depends on the C compiler. Typically, twelve is very common. However, having more than three dimensions would be more complex.
Multidimensional array example #
In the following example, first, we declare a two-dimensional array of integers with two rows and three columns. Next, we use the scanf()
function to read the number from the user’s inputs. Then, we display the array of content on the screen.
/*
* File : main.c
* Author : learnc.net
* Purpose: Demonstrates multidimensional array in C
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
const int ROW = 2;
const int COLUMN = 3;
int i,j;
int m[ROW][COLUMN];
printf("Please fill the array's content:\n");
/* fill array's elements */
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COLUMN; j++)
{
printf("\nm[%d][%d]:",i,j);
scanf("%d",&m[i][j]);
}
}
/* display array's elements */
printf("Array's content:\n");
for(i = 0; i < ROW; i++)
{
for(j = 0; j < COLUMN; j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
return 0;
}
Code language: C++ (cpp)
Initializing Multidimensional Arrays #
The following example illustrates how to initialize a multidimensional array of integers with three columns and three rows:
int matrix[3][3] =
{
{11,12,13},
{21,22,23},
{32,31,33},
};
Code language: C++ (cpp)
Multidimensional array and function #
C provides you with two ways that allow you to pass a multidimensional array to a function:
- You should always pass the number of columns to the function for two-dimensional arrays. Notice that the function does not need to know the number of rows. Only the first dimension can be omitted for three or more dimensional arrays.
- You can pass a pointer to an array to a function.
The following example demonstrates two ways of passing a multidimensional array to a function.
/*
* File : main.c
* Author : learnc.net
* Purpose: Demonstrates multidimensional array & function in C
*/
#include <stdio.h>
#include <stdlib.h>
const int ROW = 2;
const int COLUMN = 3;
void fill_array(int (*pm)[COLUMN],int row);
void display(int m[][COLUMN],int row);
int main()
{
int i,j;
int m[ROW][COLUMN];
/* fill array's elements */
fill_array(m,ROW);
/* display array's elements */
display(m,ROW);
return 0;
}
void fill_array(int (*pm)[COLUMN],int row)
{
int i,j;
printf("Please fill the array's content:\n");
/* fill array's elements */
for(i = 0; i < row; i++)
{
for(j = 0; j < COLUMN; j++)
{
printf("\nm[%d][%d]:",i,j);
scanf("%d",&pm[i][j]);
}
}
}
void display(int m[][COLUMN],int row)
{
int i,j;
/* display array's elements */
printf("Array's content:\n");
for(i = 0; i < row; i++)
{
for(j = 0; j < COLUMN; j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
}
Code language: C++ (cpp)
The following is the output of the program:
Please fill the array's content: m[0][0]:1 m[0][1]:2 m[0][2]:3 m[1][0]:4 m[1][1]:5 m[1][2]:6 Array's content: 1 2 3 4 5 6
In this tutorial, you have learned about the array, a complex type in C that allows storing multiple elements with the same data type. Next, you learned about operations that you often work with the array including declaring and initializing arrays. Then you learned how to work with multidimensional arrays in C.