Arrays in C
C supports arrays, a data structure that stores a fixed-size sequential collection of elements of the same type. Arrays are used to store a series of data, but they are often considered as a collection of variables of the same type.
Declaring an array does not involve declaring individual variables like tutorialpro0, tutorialpro1, ..., tutorialpro99, but rather declaring an array variable, such as tutorialpro, and then using tutorialpro[0], tutorialpro[1], ..., tutorialpro[99] to represent individual variables.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.
Specific elements in an array can be accessed via an index, with the first index being 0.
Declaring an Array
To declare an array in C, you need to specify the type of elements and the number of elements, as follows:
type arrayName [ arraySize ];
This is called a one-dimensional array. arraySize must be an integer constant greater than zero, and type can be any valid C data type. For example, to declare an array of 10 elements of type double named balance, the declaration statement would be:
double balance[10];
Now balance is an array that can hold 10 numbers of type double.
Initializing an Array
In C, you can initialize an array one by one or use an initialization statement, as shown below:
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
The number of values between braces { } cannot be larger than the number of elements specified in the square brackets [ ].
If you omit the size of the array, an array of the size needed to hold the initialization is created. Thus, if you write:
double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
You will create exactly the same array as in the previous example. Here is an example of assigning a value to an element of an array:
balance[4] = 50.0;
The above statement assigns the 5th element in the array a value of 50.0. All arrays have 0 as the index of their first element, which is also called the base index, and the last index of an array is the total size of the array minus 1. The following is a graphical representation of the array discussed above:
The figure below represents an array of length 10, where the index of the first element is 0, and the index of the 9th element tutorialpro is 8:
Accessing Array Elements
Array elements are accessed by using the array name followed by the index in square brackets. For example:
double salary = balance[9];
The above statement will assign the value of the 10th element in the array to the salary variable. The following example uses the concepts of array declaration, assignment, and access:
Example
#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* Initialize array elements */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* Set element at index i to i + 100 */
}
/* Print the value of each element */
for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
Detailed Explanation of Arrays in C
Arrays are very important in C, and there are several key concepts related to arrays that C programmers need to understand:
Concept | Description |
---|---|
Multidimensional Arrays | C supports multidimensional arrays. The simplest form of multidimensional array is the two-dimensional array. |
Passing Arrays to Functions | You can pass a pointer to an array by specifying the array's name without an index. |
Returning Arrays from Functions | C allows arrays to be returned from functions. |
Pointer to an Array | You can generate a pointer to the first element of an array by specifying the array's name without an index. |