Initializing Arrays in C Programming Language

Just as you can assign initial values to variables when they are declared, so can you assign initial values to the elements of an array. This is done by simply listing the initial values of the array, starting from the first element.Values  in the list are separated by commas and the entire list is enclosed in a pair of braces.

The statement

int counters[5] = { 0, 0, 0, 0, 0 };

declares an array called counters to contain five integer values and initializes each of these elements to zero. In a similar fashion, the statement

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

sets the value of integers[0] to 0, integers[1] to 1, integers[2] to 2, and so on.

Arrays of characters are initialized in a similar manner; thus the statement

char letters[5] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ };

defines the character array letters and initializes the five elements to the characters ‘a’, ‘b’, ‘c’, ‘d’, and ‘e’, respectively.

It is not necessary to completely initialize an entire array. If fewer initial values are specified, only an equal number of elements are initialized. The remaining values in the array are set to zero. So the declaration

float sample_data[500] = { 100.0, 300.0, 500.5 };

initializes the first three values of sample_data to 100.0, 300.0, and 500.5, and sets the remaining 497 elements to zero.

By enclosing an element number in a pair of brackets, specific array elements can be initialized in any order. For example,

float sample_data[500] = { [2] = 500.5, [1] = 300.0, [0] = 100.0 };

initializes the sample_data array to the same values  as shown in the previous example. And the statements

int x = 1233;

int a[10] = { [9] = x + 1, [2] = 3, [1] = 2, [0] = 1 };

define a 10-element array and initialize the last element to the value of x + 1 (or to 1234), and the first three elements to 1, 2, and 3, respectively.

Unfortunately,  C does not provide any shortcut mechanisms for initializing array ele- ments. That is, there is no way to specify a repeat count, so if it were desired to initially set all 500 values of sample_data to 1, all 500 would have to be explicitly spelled out. In such a case, it is better to initialize the array inside the program using an appropriate for loop.

Program 7.5 illustrates two types of array-initialization techniques.

Program 7.5   Initializing Arrays

#include <stdio.h>

int main (void)

{

int array_values[10] = { 0, 1, 4, 9, 16 };

int i;

for ( i = 5; i < 10; ++i )

array_values[i] = i * i;

for ( i = 0; i < 10; ++i )

printf (“array_values[%i] = %i\n”, i, array_values[i]);

return 0;

}

Program 7.5 Output

array_values[0] = 0

array_values[1] = 1

array_values[2] = 4

array_values[3] = 9

array_values[4] = 16

array_values[5] = 25

array_values[6] = 36

array_values[7] = 49

array_values[8] = 64

array_values[9] = 81

In the declaration of the array array_values, the first five elements of the array are ini- tialized to the square of their element number (for example, element number 3 is set equal to 32 or 9). The first for loop shows how this same type of initialization can be performed inside a loop. This loop sets each of the elements 5 through 9 to the square of its element number. The second for loop simply runs through all 10 elements to display their values at the terminal.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *