Node:Arrays, Next:Strings, Previous:Libraries, Up:Top
Arrays
Rows and tables of storage.
Suppose you have a long list of numbers, but you don't want to assign
them to variables individually. For example, you are writing a simple
program for a restaurant to keep a list of the amount each diner has on
his or her tab, but you don't want to go through the tedium of writing a
list like the following:
float alfies_tab, bettys_tab, charlies_tab ...; alfies_tab = 88.33; bettys_tab = 17.23; charlies_tab = 55.55; etc.
A list like that could run to hundreds or thousands of entries, and for each diner you'd have to write "special-case" code referring to every diner's data individually. No, what you really want is a single table in which you can find the tab corresponding to a particular diner. You can then look up the tab of the diner with dining club card number 7712 in row number 7712 of the table much more easily.
This is why arrays were invented. Arrays are a convenient way to group many variables under a single variable name. They are like pigeonholes, with each compartment storing a single value. Arrays can be one-dimensional like a list, two-dimensional like a chessboard, or three-dimensional like an apartment building -- in fact, they can have any arbitrary dimensionality, including ones humans cannot visualise easily.
An array is defined using square brackets [...]
. For example: an
array of three integers called my_list
would be declared thus:
int my_list[3];
This statement would cause space for three adjacent integers to be
created in memory, as in the diagram below. Notice that there is no
space between the name of the array above (my_array
) and the
opening square bracket [
.
------------------------------------ my_list: | | | | ------------------------------------
The number in the square brackets of the declaration is referred to as the subscript of the array, and it must be an integer greater than or equal to zero.
The three integer "pigeonholes" in the above array are called its locations, and the values filling them are called the array's elements. The position of an element in the array is called its index (the plural is indices). In the following example, 5, 17, and 23 are the array's elements, and 0, 1, and 2 are its corresponding indices.
Notice also that although we are creating space for three integers,
arrays in C are zero-based, so the indices of the array run (0, 1,
2). If arrays in C were one-based, the indices would run (1, 2, 3).
int my_list[3]; my_list[0] = 5; my_list[1] = 17; my_list[2] = 23;
The above example would result in an array that "looks like" the
following diagram. (Of course, an array is merely an arrangement of
bytes in the computer's memory, so it does not look like
much of anything, literally speaking.)
index: 0 1 2 ------------------------------------ my_list: | 5 | 17 | 23 | ------------------------------------
Note that 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. However, if you want an array with multiple data
types, you might instead be able to use multiple arrays of different
data types that contain the same number of elements. For example, to
continue our restaurant tab example above, one array, diner_names
might contain a list of the names of the diners. If you are looking for
a particular diner, say Xavier Nougat, you might find that the index of
his name in diner_names
is 7498. If you have programmed an
associated floating-point array called diner_tabs
, you might look
up element 7498 in that array and find that his tab is $99.34.