.TH array "" "" "Definition"
.PC
.PP
An
.B 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.
.PP
For example, the array
.B "int foo[3]"
has three elements, each of which is an \fBint\fR.
The three \fBint\fRs are stored consecutively in memory, and each can be
addressed by the array name \fBfoo\fR plus a subscript that indicates
its place within the array, as follows:
.BR foo[0] ,
.BR foo[1] ,
and
.BR foo[2] .
Note that the numbering of elements within an array always
begins with \*(Ql0\*(Qr.
.PP
Arrays, like other data elements, may be automatic (\fBauto\fR),
.BR static ,
or
external (\fBextern\fR).
.PP
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
.B 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
.B "foo[][10]"
is acceptable, whereas
.B "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.
.PP
.II initialization
You can initialize automatic arrays and structures,
provided that you know the size of the array,
or of any array contained within a structure.
.II aggregate
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.
.SH "Flexible Arrays"
.II "flexible arrays"
A
.B "flexible array"
is one whose length is not declared explicitly.
Each has exactly one empty \*(Ql[\ ]\*(Qr array-bound
declaration.
If the array is multidimensional,
the flexible dimension of the array must be the \fIfirst\fR
array bound in the declaration;
for example:
.DM
	int example1[][20];	/* RIGHT */
	int example2[20][];	/* WRONG */
.DE
.PP
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.
.PP
Flexible arrays occur in only a few contexts; for example,
as parameters:
.DM
	char *argv[];
	char p[][8];
.DE
.PP
as
.B extern
declarations:
.DM
	extern int end[];
.DE
.PP
or as a member of a structure \(em usually,
though not necessarily, the last:
.DM
	struct nlist {
		struct nlist *next;
		char name[];
	};
.DE
.SH Example
The following program initializes an automatic array, and prints
its contents.
.DM
main()
{
	int foo[3] = { 1, 2, 3 };
.DE
.DM
	printf("Here's foo's contents: %d %d %d\en",
		 foo[0], foo[1], foo[2]);
}
.DE
.SH "See Also"
.Xr initialization, initializ
.Xr "Programming COHERENT," programmi
.Xr struct struct
