

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  iinntt.  The  three iinntts  are stored  consecutively in
memory, and  each can be addressed  by the array name  ffoooo 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  (aauuttoo),
static, or external (eexxtteerrnn).

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


