Previous: 7.7 Common Errors
Up: 7 Arrays
Next: 7.9 Exercises
Previous Page: 7.7 Common Errors
Next Page: 7.9 Exercises

7.8 Summary

In this Chapter, we have introduced one form of compound data type: the array. An array is a block of a number of data items all of the same type allocated in contiguous memory cells. We have seen that, in C, an array may be declared using the syntax:

specifying the type of the data items, and the number of elements to be allocated in the array. As we saw, such a declaration in a function causes <size> data items of type <type-specifier> to be allocated in contiguous memory, AND a pointer cell to be allocated of type -specifier>* (pointer to <type-specifier>), given the name, , and initialized to point to the first cell of the array. More specifically, for a declaration like:
int data[100];
allocates 100 int cells, and an int * cell named data which is initialized to point to the block of integers.

We saw that the data items in an array can be accessed using an index; i.e. the number of the item in the block. Numbering of data items begins with index 0, to the size - 1. We use the index of an element in a subscripting expression with syntax:

where <identifier> is the name of the array, and the <expression> in the square brackets ( []) is evaluated to the index value. So, for our previous example, the statement:
data[0] = 5;
sets the integer value, 5, into the first cell of the array, data. While:
data[i] = data[i-1];
would copy the value from the element with index i - 1 to its immediate successor in the array.

The data types of the elements of an array may be any scalar data type; int, float, or char. (We will see other types for array elements in later chapters). We have cautioned that, in C, no checking is done on the subscripting expressions to ensure that the index is withing the block of data allocated (i.e. that the subscript is in bounds). It is the programmers responsibility to ensure the subscript is in bounds. We have seen two ways of doing this: to keep the value of the limit or the extent of data in the array in a separate integer variable and perform the necessary comparisons, or to mark the last item in the array with a special value. The most common use of this later method is in the case of an array of characters (called a string), where the end of the string is indicated with the special character, NULL (whose value is 0).

We have also discussed the equivalence of subscripting expressions and pointer arithmetic; i.e. that a subscripting expression, data[i], is equivalent to (and treated by the compiler as) the pointer expression, *(data + i). Remember, the name of the array is a pointer variable, pointing to the first element of the array. These two forms of array access may be used interchangebly in programs, as fits the logic of the operation being performed. It is the semantics of pointer arithmetic that will compute the address of the indexed element correctly.

In addition, we have seen that passing arrays as parameters to functions is done by passing a pointer to the array (the array name). The cells of data are allocated in the calling function, and the called function can access them indirectly using either a pointer expression or a subscripting expression. Remember, a parameter like:

int func( int a[] )
(even if it has a <size> in the brackets) does NOT allocate integer cells for the array; it merely allocates an int * cell which will be given a pointer to an array in the function call. Such a parameter is exactly equivalent to:
int func( int *a)

We have discussed the fact that the pointer cell, referenced using the name of the array, is a constant pointer cell; i.e. it may not be changed by the program (it may not be used as an Lvalue). However; additional pointer cells of the appropriate type may be declared and initialized to point to the array (by the programmer) and can then be used to traverse the array with pointer arithmetic (such as the ++ or -- operators).

We have shown how arrays can be initialized in the declaration (a bracketed, comma separated list of values, or, for strings, a string constant). We have seen the semantics of string assignment and how strings can be read and printed by scanf() and printf() using the %s conversion specifier. Remember, for scanf(), %s behaves like the numeric conversion specifiers; it skips leading white space and terminates the string (with a NULL) at the first following white space character.

Finally, we have shown an example of using arrays in a data base type applications, where arrays of different types were used to hold a collection of payroll records for individuals. In that example, the elements at a specific index in all of the arrays corresponded to one particular data record.

The array is an important and powerful data structure in any programming language. Once you master the use of arrays in C, the scale and scope of your programming abilities expand tremendously to include just about any application.



Previous: 7.7 Common Errors
Up: 7 Arrays
Next: 7.9 Exercises
Previous Page: 7.7 Common Errors
Next Page: 7.9 Exercises

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:56:22 HST 1994