COHERENT manpages

This page displays the COHERENT manpage for array [Definition].

List of available manpages
Index


array -- Definition

An array is a concatenation of  data elements, all of which are of the same
type.  All the elements of an array are stored consecutively in memory, and
each element  within the array  can be addressed  by the array  name plus a
subscript.

For example, the  array int foo[3] has three elements,  each of which is an
int.  The  three ints are stored  consecutively in memory, and  each can be
addressed by the  array name foo 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  (auto), static,  or
external (extern).

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, the  form foo[][10] is acceptable, whereas  foo[10][] is not.  The
first form is an indefinite  number of ten-element arrays, which is correct
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  manner  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 first
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 array 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:

    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

initialization,
Programming COHERENT,
struct