|
|
coherent
array Definition array
An array is a concatenation of data elements, all of which are of
the same type. All the elements of an array are stored consecu-
tively in memory, and each element within the array can be ad-
dressed by the array name plus a subscript.
For example, the array int foo[3] has three elements, each of
which is an iinntt. The three iinntts are stored consecutively in
memory, and each can be addressed by the array name ffoooo plus a
subscript that indicates its place within the array, as follows:
foo[0], foo[1], and foo[2]. Note that the numbering of elements
within an array always begins with `0'.
Arrays, like other data elements, may be automatic (aauuttoo),
static, or external (eexxtteerrnn).
Arrays can be multi-dimensional; that is to say, each element in
an array can itself be an array. To declare a multi-dimensional
array, use more than one set of square brackets. For example,
the multi-dimensional array foo[3][10] is a two-dimensional array
that has three elements, each of which is an array of ten
elements. The second sub-script is always necessary in a multi-
dimensional array, whereas the first is not. For example,
foo[][10] is acceptable, whereas foo[10][] is not. The first
form is an indefinite number of ten-element arrays, which is cor-
rect C, whereas the second form is ten copies of an indefinite
number of elements, which is illegal.
You can initialize automatic arrays and structures, provided that
you know the size of the array, or of any array contained within
a structure. An automatic array is initialized in the same man-
ner as aggregate, but initialization is performed on entry to the
routine at run time, instead of at compile time.
***** Flexible Arrays *****
A flexible array is one whose length is not declared explicitly.
Each has exactly one empty `[ ]' array-bound declaration. If the
array is multidimensional, the flexible dimension of the array
must be the _f_i_r_s_t array bound in the declaration; for example:
int example1[][20]; /* RIGHT */
int example2[20][]; /* WRONG */
The C language allows you to declare an indefinite number of ar-
ray elements of a set length, but not a set number of array
elements of an indefinite length.
Flexible arrays occur in only a few contexts; for example, as
parameters:
COHERENT Lexicon Page 1
array Definition array
char *argv[];
char p[][8];
as extern declarations:
extern int end[];
or as a member of a structure -- usually, though not necessarily,
the last:
struct nlist {
struct nlist *next;
char name[];
};
***** Example *****
The following program initializes an automatic array, and prints
its contents.
main()
{
int foo[3] = { 1, 2, 3 };
printf("Here's foo's contents: %d %d %d\n",
foo[0], foo[1], foo[2]);
}
***** See Also *****
definitions, struct
_T_h_e _C _P_r_o_g_r_a_m_m_i_n_g _L_a_n_g_u_a_g_e, ed. 2, pages 25, 83, 210
COHERENT Lexicon Page 2
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.