Sie sind auf Seite 1von 5

C Arrays

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.

The general form to declare an array is as follows:

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.

NOTE that a subscript must be an integer value or an expression that is evaluated to


the integer value. In the latter case the expression will be evaluated first, and the
value will be substituted as an index into the array next. For example, the following are
valid references to the elements of the arrays: numbers[ i+1 ], temperature[
scale(i) ], result[ numbers[i] ]. In the last example the element of the integer
array numbers[] is used as a subscript of the array result[].

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:

int numbers[5] = {1, 2, 3, 4, 5};


double x[10] = {0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0};
char word[] = {'w', 'o', 'r', 'd'};

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.

Accessing Elements of Arrays

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.

/* Initialise and print elements of the array items_code[] */


for(i = 0; i < NUMBER_OF_ITEMS; i++) {
items_code[i] = i*3 + 1;
printf("Item number %d, code %d ", i, items_code[i]);
}

If an array has not been initialised, all elements are initialised to zero
values by default.

Initialisation to zero values can be done in a short way as follows:

int num[MAX_NUMBERS] = {0};

The example below ilustrates the declaration, initialisation and use of an array.

Program 8.1

A survey was done on 30 people to establish their opinion regarding a popular


newspaper. People were asked to rate the quality of the publications on a
scale of 1 to 10, where 1 means very bad and 10 means excellent. Write a
program that reads responses from the keyboard, and summarises and
displays the number of responses of each type.

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.

/*This program reads responses of the survey from the keyboard,


and summarises and displays the number of responses of each type.*/

/*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};

/*Read responses from the keyboard and store


in the array responses[] */
for(i = 0; i < MAX_RESPONSES; i++) {

/*read an input and check that it is an integer


value between 1 and 10*/
k = scanf("%d", &responses[i]);
while(k != 1 || responses[i] <= 0 || responses[i] > 10) {
error_message();
k = scanf("%d", &responses[i]);
}

/*increment the element of the array rating[]


according to the value of the array responses[]*/
++rating[responses[i] - 1];
}

/*print the results */


printf("%s%10s\n", "Rating", "Frequency");
for(j = 1; j < MAX_RATING; j++) {
printf("%d%10d\n", j+1, rating[j]);
}

system("PAUSE");
return 0;
}

3
void error_message() {

printf("Error. Please enter an integer number between 1 and 10");


while(getchar() != NULL); /*retrieve characters from the buffer */
}

In this program we used the elements of the array responses[] as subscripts of the
array rating[].

Passing Arrays to Functions

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:

void get_frequencies(int rating[]) {


int responses[MAX_RESPONSES]; /* local array */
int j, i;

for(i = 0; i < MAX_RESPONSES; i++) {


/*read an input and check that it is an integer
value between 1 and 10*/
k = scanf("%d", &responses[i]);
while(k != 1 || responses[i] <= 0 || responses[i] > 10) {
error_message();
k = scanf("%d", &responses[i]);
}

/*increment the element of the array rating[]


according to the value of the array responses[]*/
++rating[responses[i] - 1];
}
}

The name of an array refers to the memory location of the first


element of the array, that is the name of the array is the
address of the first element. Thus, passing the address of the
first element to a function, we make the whole array available to
this function. This is called pass by reference;.
When the function modifies array elements in its function body, it is
accessing the actual elements of the array in their original memory
locations.

Alternatively, when a variable is passed to a function, it is passed by

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,

void print_array(const int array[], int size)


{...
}

If the function attempts to change values in the array passed with a


const qualifier, the compile-time error occurs.

Das könnte Ihnen auch gefallen