login

C Array

Array Definition

Array by definition is a variable that hold multiple elements which has the same data type.

Declaring Arrays

We can declare an array by specifying its data type, name and the fixed number of elements the array holds between square brackets immediately following the array name.

The following illustrates how to declare an array:

data_type array_name[size];

For example, to declare an array that contains 100 integer numbers you can do as follows:

int a[100];

You must follow the rules below when you declare an array in C:

  • The data type can be any valid C data types including C structure and union.
  • The name of an array has to follow the rule of C variables.
  • The size of array has to be a positive constant integer.

Initializing Arrays

It is like a variable, an array can be initialized. To initialize an array, you provide initializing values which are enclosed within curly braces in the declaration and placed following an equals sign after the array name.

Here is an example of initializing an array of integers.

int list[5] = {2,1,3,7,8};

Accessing Array Element

You can access array elements via indexes like array_name[index].

Indexes of array starts from 0 not 1 so the highest elements of an array is array_name[size-1].

Array and Pointer

An array is a pointer to the 0th element of the array. When you dreference the array name you will get the 0th element. This give us the possibility of accessing array's element not only via index but also via pointer.

It is note that the array is treated as constant so you can only modify the values in the array but not array itself.

The program below will help you visualize the memory address each array's element and how to access array's elements using pointer.

#include <stdio.h>
 
void main()
{
    
    const int size = 5;
    
    int list[size] = {2,1,3,7,8};
    
    int* plist = list;
 
    // print memory address of array elements
    for(int i = 0; i < size;i++)
    {
        printf("list[%d] is in %d\n",i,&list[i]);
    
    }
 
    // accessing array elements using pointer
    for(i = 0; i < size;i++)
    {
        printf("list[%d] = %d\n",i,*plist);
        
        /* increase memory address of pointer so it go to the next 
           element of the array */
        plist++;
    }
 
}

Here is the output

list[0] is in 1310568
list[1] is in 1310572
list[2] is in 1310576
list[3] is in 1310580
list[4] is in 1310584
list[0] = 2
list[1] = 1
list[2] = 3
list[3] = 7
list[4] = 8

You can store pointers in an array and in this case we have an array of pointers. The following illustrates an array of pointers.

	int *ap[10];

Multidimensional Arrays

An array with more than one index value is called a multidimensional array. All the arrays you used above are called single-dimensional arrays.

To declare a multidimensional array you can do as follows:

data_type array_name[][][];

The number of square brackets specifies the dimension of the array. For example to declare two dimensions integer array we can do as follows:

int matrix[3][3];

Initializing Multidimensional Arrays

You can initialize an array as a single-dimension array. Here is an example of initializing an two dimensions integer array:

int matrix[3][3] = 
{
  {11,12,13},
  {21,22,23},
  {32,31,33},
};