Declaring Arrays
Arrays are used to store and process large amounts of homogeneous data (data of the
same type). Each item in an array is called an element. All elements of an array are
referenced by the name of the array and are stored in a set of consecutive memory
locations.
data-type array-name[array-size];
Here the data-type is the type of elements of the array, array-name is the name of the
declared array, and the array-size defines the number of the elements in this array.
For example, the statement int numbers[100] declares an array of integers with 100
elements; double temperature[10] declares an array of size 10 the elements of
which are double precision values etc.
Each element in an array can be referred to using the array name and the position
number of this element. The position number contained within square brackets is called
an index or a subscript.
The index of the first element in every array is zero. Thus, the first
element of the array numbers[] is numbers[0], the second is numbers[1], the last
element is numbers[99].
If the size of the array is N, then the index of the last element is N-1.
1
Elements of the array must be initialised before they are used. If the array is small, it
can be initialised at the same time as it is declared. For example, the following
statements declare arrays and initialise their elements:
NOTE, that you can ommit the number of elements in the array declaration if you
initialise its elements in the same statement. The number of elements in the array will
be the number of elements in the initialiser list.
To access all elements of the array in order to process or display them, the for loop is
usually used. For example, the following code fragment initialises elements of the array
items_code[NUMBER_OF_ITEMS] and displays them on the screen.
If an array has not been initialised, all elements are initialised to zero
values by default.
The example below ilustrates the declaration, initialisation and use of an array.
Program 8.1
2
This problem is a typical array application. The array responses[30] will store all
responses, and the array rating[10] will accumulate the numbers of each type of
response. Note that the number of occurences of the rating 1 will be stored in the
element rating[0], rating 2 - in the element rating[1], etc.
/*Version 1.*/
#include
#define MAX_RESPONSES 30
#define MAX_RATING 10
void error_message();
int main() {
int i, j, k;
int responses[MAX_RESPONSES];
int rating[MAX_RATING] = {0};
system("PAUSE");
return 0;
}
3
void error_message() {
In this program we used the elements of the array responses[] as subscripts of the
array rating[].
To pass an array as a parameter to a function we pass only the name of the array
without any brackets. Usually the size of the array is also passed unless it is defined
using #define statement. For example, in the Program 2 we may write a function which
reads the inputs and calculates frequencies of all responses as follows:
4
value (the function recieves a copy of the varialbe). Any changes made
to the variable within the function body will not change its original value.
Sometimes you might not want to allow the function to modify array’s
elements. C language provides the type modifier const to prevent
modification of array elements in a function. For example,