Summary: in this tutorial, you will learn about the C array, how to declare arrays and how to manipulate array’s elements effectively.
C Array Definition
C array is a variable that holds multiple elements which share the same data type.
Declaring C Arrays
In order to declare an array, you need to specify:
- The data type of the array’s elements. It could be int, float, char, etc.
- The name of the array.
- A fixed number of elements that array may contain. The number of elements is placed inside square brackets followed the array name.
The following illustrates the typical syntax of declaring an array:
1 | data_type array_name[size]; |
For example, you can declare an array that holds 10 integers as follows:
1 | int a[10]; |
When you declare an array, the compiler allocates a memory block that holds the entire array. The array elements are stored sequentially in the memory as the following picture:
You must follow the following rules when you declare an array in C:
- The data type can be any valid data type such as int, float, char, structure or union.
- The name of an array must follow naming rules of variables.
- The size of the array must be zero or a constant positive integer.
Accessing array elements
You can access an individual element of the array by using the array name followed by the element subscript enclosed in square brackets as follows:
1 | array_name[subscript] |
Notice that the array elements start from 0
, not 1
, so the first element of the a
array is a[0]
and the last element is a[size-1]
where size
is the number of element in the a
array.
The following program demonstrates how to access elements of an array:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <stdio.h> int main() { const int SIZE = 5; int a[SIZE]; int i; for(i = 0; i < SIZE; i++) { a[i] = i; printf("a[%d] = %d\n",i,a[i]); } } |
Initializing arrays
You can initialize an array like a variable. To initialize an array, you provide default values enclosed in curly braces in the declaration part and assign it to the array. The following example initializes an array of 5 integers.
1 | int a[5] = {2,1,3,7,8}; |
If you omit the size of the array, the compiler will create an array with the size that is sufficient enough to hold the initialized values. See the following example:
1 | int a[] = {1,2,3}; |
The compiler will create an array with the size of three.
If you initialize just only few array elements, you will not know exact values of the uninitialized elements when the program executes e.g.
1 | int a[5] = {0,5,3}; |
The a[3]
and a[4]
can hold any integer values.
Array and Pointer
The array name is equivalent to a pointer that points to the first element in the array. When you dereference the array name, you get the value of the first element.
For example, when you declare:
1 | int a[10]; |
You are actually declaring a pointer named a
that points to the first element in the array i.e., a
is exactly is &a[0]
1 2 3 4 5 6 7 8 9 | #include <stdio.h> int main() { int a[10]; printf("a = %p\n", a); printf("&a[0] = %p\n",&a[0]); } |
The output is as we expected:
1 2 | a = 0028FEF8 &a[0] = 0028FEF8 |
However the array name is treated as a constant pointer i.e., you cannot change the memory address where it points to. The expression a[i] is equivalent to *(a + i) . This gives you the possibility of accessing elements of the array using not only a subscript but also a pointer.
The following program helps you understand more about accessing elements of an array using a pointer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> #define SIZE 5 int main() { int a[SIZE] = {2,1,3,7,8}; int i; int* pa = a; for(i = 0; i < SIZE; i++) { printf("a[%d] = %d\n",i,*pa); pa++; } return 0; } |
The following is output:
1 2 3 4 5 | a[0] = 2 a[1] = 1 a[2] = 3 a[3] = 7 a[4] = 8 |
C Multidimensional Array
Declare C multidimensional array
C multidimensional array has more than one subscript. To declare a multidimensional array, you use the following syntax:
1 | type name[size][size2][size3]… |
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 C compiler. Typically, twelve is very common. However, it would be more complex if you have more than three dimensions.
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 user’s inputs. Then, we display the array content on the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | /* * File : main.c * Author : zentut.com * 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; } |
Initializing Multidimensional Arrays
The following example illustrates how to initialize a multidimensional array of integers with three columns and three rows:
1 2 3 4 5 6 | int matrix[3][3] = { {11,12,13}, {21,22,23}, {32,31,33}, }; |
Multidimensional array and function
C provides you with two ways that allow you to pass a multidimensional array to a function:
- For two-dimensional arrays, you should always pass the number of columns to the function. Notice that function does not need to know a number of rows. For three or more dimensional array, only the first dimension can be omitted.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | /* * File : main.c * Author : zentut.com * 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"); } } |
The following is the output of the program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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, which is a complex type in C that allows you to store multiple elements with the same data type. Next, you learned about some common operations that you often work with the array including declaring and initializing arrays. Then you learned how to work with multidimensional arrays in C.