What Is an Array?
You now know how to declare a variable with a specified data type, such as char, int, float, or double. In many cases, you have to declare a set of variables that have the same data type. Instead of declaring them individually, C allows you to declare a set of variables of the same data type collectively as an array.
An array is a collection of variables that are of the same data type. Each item in an array is called an element. All elements in an array are referenced by the name of the array and are stored in a set of consecutive memory slots.
Declaring Arrays
The following is the general form to declare an array:
data-type Array-Name[Array-Size];
Here data-type is the type specifier that indicates what data type the declared array will be. Array-Name is the name of the declared array. Array-Size defines how many elements the array can contain. Note that the brackets ([ and ]) are required in declaring an array. The bracket pair ([ and ]) is also called the array subscript operator.
For example, an array of integers is declared in the following statement,
int array_int[8];
where int specifies the data type of the array whose name is array_int. The size of the array is 8, which means that the array can store eight elements (that is, integers in this case).
In C, you have to declare an array explicitly, as you do for other variables, before you can
use it.
Indexing Arrays
After you declare an array, you can access each of the elements in the array separately.
For instance, the following declaration declares an array of characters:
char day[7];
You can access the elements in the array of day one after another.
The important thing to remember is that all arrays in C are indexed starting at 0. In other words, the index to the first element in an array is 0, not 1. Therefore, the first element in the array of day is day[0]. Because there are 7 elements in the day array, the last element is day[6], not day[7].
The seven elements of the array have the following expressions: day[0], day[1], day[2], day[3], day[4], day[5], and day[6].
Because these expressions reference the elements in the array, they are sometimes called array element references.
Initializing Arrays
With the help of the array element references, you can initialize each element in an array.
For instance, you can initialize the first element in the array of day, which was declared in the last section, like this:
day[0] = `S';
Here the numeric value of S is assigned to the first element of day, day[0].
Likewise, the statement day[1] = `M'; assigns `M' to the second element, day[1], in the array.
The second way to initialize an array is to initialize all elements in the array together. For instance, the following statement initializes an integer array, arInteger:
int arInteger[5] = {100, 8, 3, 365, 16};
Here the integers inside the braces ({ and }) are assigned to the corresponding elements of the array arInteger. That is, 100 is given to the first element (arInteger[0]), 8 to the second element (arInteger[1]), 3 to the third (arInteger[2]), and so on.
Listing 12.1 gives another example of initializing arrays.
TYPE
Listing 12.1. Initializing an array.1: /* 12L01.c: Initializing an array */
2: #include <stdio.h>
3:
4: main()
5: {
6: int i;
7: int list_int[10];
8:
9: for (i=0; i<10; i++){
10: list_int[i] = i + 1;
11: printf( "list_int[%d] is initialized with %d.\n", i, list_int[i]);
12: }
13: return 0;
14: }
OUTPUT
The following output is displayed on the screen after the executable (12L01.exe) of the program in Listing 12.1 is created and run from a DOS prompt:
C:\app>12L01
list_int[0] is initialized with 1.
list_int[1] is initialized with 2.
list_int[2] is initialized with 3.
list_int[3] is initialized with 4.
list_int[4] is initialized with 5.
list_int[5] is initialized with 6.
list_int[6] is initialized with 7.
list_int[7] is initialized with 8.
list_int[8] is initialized with 9.
list_int[9] is initialized with 10.
C:\app>
ANALYSIS
As you can see in Listing 12.1, there is an integer array, called list_int, which is declared in line 7. The array list_int can contain 10 elements.
Lines 9_12 make up a for loop that iterates 10 times. The statement in line 10 initializes list_int[i], the ith element of the array list_int, with the value returned from the i + 1 expression.
Line 11 then prints out the name of the element, list_int[i], and the value assigned to the element.
- 18 views