Documente Academic
Documente Profesional
Documente Cultură
int array[3];
This statement would cause space for three
adjacent integers to be created in memory, as in
the diagram . There is no space between the
name of the array (array) and the opening
square bracket ‘[’.
------------------------------------
array | | | |
------------------------------------
The number in the square brackets of the
The three integer in the above array are called
itslocations, and the values filling them
are called the array’s elements. The
position of an element in the array is called its
index ( indices). In the example, 5, 17,
and 23 are the array’s elements, and 0, 1, and
2 are its corresponding indices.
We are creating space for three integers,
arrays in C are zerobased,
so the indices of the array run (0, 1, 2).
int array[3];
array[0] = 5;
array[1] = 17;
The example would result in an array that
“looks like” . (An array is an arrangement of
bytes in the computer’s memory )
index: 0 1 2
------------------------------------
array: | 5 | 17 | 23 |
------------------------------------
Every element in an array must be of the same
type, for example, integer. It is not possible in
C to have arrays that contain multiple data
types. If we want an array with multiple data
types, we might use multiple arrays of
different data types that contain the same
number of elements. For example, our
restaurant tab example , one array,
If we are looking for a particular diner, say Anjus,
we might find that the index of her name in
diner_names is 7498. If we have programmed an
associated floating-point array called diner_tabs,
we might look up element 7498 in that array and
find that her tab is $99.34.
Array bounds
The first and last positions in an array are called
its bounds. Bounds of an array are zero and the
integer that equals the number of elements it
contains, minus one.