Chapter 9 Arrays
Chapter 9 Arrays
Chapter 9
Introduction (1/2)
Many applications require the processing of multiple data
items that have common characteristics (e.g., a set of
numerical data, represented by x1, x2, . . . ,xn).
In such situations it is often convenient to place the data
items into an array, where they will all share the same
name (e.g., x).
However, they must all be of the same type and the same
storage class.
Each array element (i.e., each individual data item) is
referred to by specifying the array name followed by one or
more subscripts, with each subscript enclosed in square
brackets.
Each subscript must be expressed as a nonnegative integer.
In an n-element array, the array elements are X[0], X[1] ,
X[2], . . . ,X[n – 1] , as illustrated in Fig. 9.1.
The value of each subscript can be expressed as an integer
constant, an integer variable or a more complex integer
expression.
Introduction (2/2)
The number of subscripts determines the
dimensionality of the array. For example, x [ i ]
refers to an element in the one-dimensional array
x. Similarly, y[ i ][ j ] refers to an element in the
two-dimensional array y.
Higher-dimensional arrays can be also be formed,
by adding additional subscripts in the same
manner (e.g., z [ i ] [j] [ k ] )
DEFINING AN ARRAY
In general terms, a one-dimensional array
definition may be expressed as storage -class
data- type array[ expression] ;
It is sometimes convenient to define an array size
in terms of a symbolic constant rather than a
fixed integer quantity.
The initial values must appear in the order in
which they will be assigned to the individual array
elements, enclosed in braces and separated by
commas. The general form is
storage- class data- type array[ expression] =
{ value 1, value 2, . . . , value n) ;
DEFINING AN ARRAY
DEFINING AN ARRAY
All individual array elements that are not
assigned explicit initial values will automatically
be set to zero.
This includes the remaining elements of an array
in which some elements have been assigned
nonzero values.
DEFINING AN ARRAY
The array size need not be specified explicitly
when initial values are included as a part of an
array definition.
With a numerical array, the array size will
automatically be set equal to the number of initial
values included within the definition.
In particular, when a string constant is assigned
to an external or a static character array as a part
of the array definition, the array size specification
is usually omitted.
The proper array size will be assigned
automatically. This will include a provision for the
null character \O, which is automatically added at
the end of every string
PROCESSING AN ARRAY
Single operations which involve entire arrays are not
permitted in C. Thus, if a and b are similar arrays
(i.e., same data type, same dimensionality and same
size), assignment operations, comparison operations,
etc. must be carried out on an element-by-element
basis.
This is usually accomplished within a loop, where
each pass through the loop is used to process one
array element. The number of passes through the
loop will therefore equal the number of array
elements to be processed.
Let us define list to be a 100-element, floating-point
array. However, we need not make use of all 100
elements. Rather, we shall specify the actual number
of elements by entering a positive integer quantity
PASSING ARRAYS TO
FUNCTIONS
An entire array can be passed to a function as an
argument.
To pass an array to a function, the array name
must appear by itself, without brackets or
subscripts, as an actual argument within the
function call.
When declaring a one dimensional array as a
formal argument, the array name is written with a
pair of empty square brackets.
The size of the array is not specified within the
formal argument declaration.
PASSING ARRAYS TO
FUNCTIONS
We have already discussed the fact that arguments are
passed to a function by value when the arguments are
ordinary variables
When an array is passed to a function, however, the
values of the array elements are not passed to the
function.
Rather, the array name is interpreted as the address
of the first array element (i.e., the address of the
memory location containing the first array element).
This address is assigned to the corresponding formal
argument when the function is called. The formal
argument therefore becomes a pointer to the first
array element (more about this in the next chapter).
Arguments that are passed in this manner are said to
be passed by reference rather than by value.
PASSING ARRAYS TO
FUNCTIONS