An array holds a fixed number of similar elements that are stored under one name. These elements are stored in contagious memory locations. It is one of the simplest data structures. Most modern programming languages have arrays built-in by default.
An array has to be declared before it can be used. In C, declaring an array means specifying the following:
Data Type: This is the kind of values that the array will store. This can be characters, integers, floating points or any legal data type.
Name: The variable name used to identify the array and interact with it.
Size: The size of the array, which specifies the maximum number of values that the array will store.
Syntax Used
An array can be declared in C by using the following syntax:
type name[size];
By default, an array is created whenever memory is available at any random location. We do not know what information that random location of memory will contain, as any other program could have used that memory previously.
If array elements are not initialized while creation, then accessing them directly they would result in such garbage values.
Therefore, it is always recommended to empty the elements or assign values to it if a calculation is to be performed on the array.
int ages[10];
// accessing array without assigning elements first
for(int i = 0; i < 10; i++)
printf("\n arr[%d] = %d", i, ages[i]);
Inserting an element at the end of the array is easy provided the array has enough space for the new element. The index of the last element of the array is found out and the new element is inserted at the index + 1 position.
An element can be inserted in between at any position by shifting all elements from that position to the back of the array. The element to be inserted is then inserted at the required position.
void insert_position(int arr[]) {
int i = 0, pos, num;
printf("Enter the number to be inserted : ");
scanf("%d", &num);
printf("Enter position at which the number is to be added :");
scanf("%d", &pos);
for (i = n-1; i>= pos; i--)
arr[i+1] = arr[i];
arr[pos] = num;
n = n + 1; // increase total number of used positions
display_array(arr);
}
An array may have more than one dimension to represent data. These are known as multidimensional arrays. The elements in these arrays are accessed using multiple indices.
A two dimensional array can be considered as an array within an array. It can be visualised as a table, having a row and a column. Each item in the table can be accessed using 2 indices corresponding to the row and column.
A 2D array is declared using 2 parameters:
type name[max_size_x][max_size_y]
The max_size_x and max_size_y are the max values each dimension can store.
A three dimensional array similarly can be visualised as a cube. Each item can be accessed using 3 indices corresponding to the 3D position.
Example: Every block of a Rubik’s Cube can be represented by a three dimensional array of size 3 x 3 x 3.
A 3D array is declared using 2 parameters:
type name[max_size_x][max_size_y][max_size_z];
The max_size_x, max_size_y and max_size_z are the max values each dimension can store.
Since an array stores all its data elements in consecutive memory locations, storing just the base address, that is, the address of the first element is sufficient. The address of other elements can be then calculated using the base address.
A simple formula consisting of the size of the element and the lower bound is used.
A[i] = base_address(A) + size_of_element(i – lower_bound)
The lower bound is the smallest index in the array. Similarly, an upper bound is the largest index in the array. In C programming, the lower bound value may be omitted as it is generally 0.
The elements in a two dimensional array can be stored using 2 representations and their addresses can be calculated using the respective formulae.
In this form, the elements are stored column by column. m elements of the first column are stored in the first m locations, elements of the second column element are stored in the next m locations, and so on.
Address(A[I][J]) = base_address + width {number_of_rows (J – 1) + (I – 1)}
In this form, the elements are stored row by row. n elements of the first row are stored in the first n locations, elements of the second row elements are stored in the next n locations, and so on.
Address(A[I][J]) = base_address + width {number_of_cols (I – 1) + (J – 1)}