Arrays

An array is an ordered group of data objects. Each object is called an element. All elements within an array have the same data type.

Using any type specifier in an array definition or declaration. Array elements can be of any data type, except function or, in C++, a reference. You can, however, declare an array of pointers to functions.

Syntax

 

You can leave the first (and only the first) set of subscript brackets empty in

The following example defines a two-dimensional array that contains six elements having type char:

char list[3][2];

Multidimensional arrays are stored in row-major order. Individual elements of list would be:

list[0][0]
list[0][1]
list[1][0]
list[1][1]
list[2][0]
list[2][1]

Initializing Arrays
The initializer for an array contains the = symbol followed by a comma-separated list of constant expressions enclosed in braces ({}).

The following show four different character array initializations:

static char name1[] = { 'J', 'a', 'n' };
static char name2[] = { "Jan" };
static char name3[3] = "Jan";
static char name4[4] = "Jan";

These initializations create the following elements:

Element Value Element Value Element Value Element Value
"name1[0]" "J" "name2[0]" "J" "name3[0]" "J" "name4[0]" "J"
"name1[1]" "a" "name2[1]" "a" "name3[1]" "a" "name4[1]" "a"
"name1[2]" "n" "name2[2]" "n" "name3[2]" "n" "name4[2]" "n"
    "name2[3]" "\0"     "name4[3]" "\0"

Note that the NULL character is lost for name1[] and name3 [3]. In C, a compiler warning is issued for name3[3 ]. In C++, the compiler issues a severe error for name3[3 ].

Initializing a Multidimensional Array
The following definitions show two ways of initializing a two-dimensional array:

static int matrix[3][2] = { 1, 2,
			    3, 4,
			    5, 6 };
	OR

static int matrix[3][2] = {{1,2},{3,4},{5,6}};

Both methods are valid but it is preferable to use nested braces to initialize dimensions and elements in a dimension selectively.

You cannot have more initializers than number of elements in the array.

Zero Extent Arrays in Structures and Unions
Zero-extent arrays are permitted in structures and unions under extended mode. Extended mode can be set by specifying the compiler level language option.

Zero-extent arrays are arrays that have a size of 0 or no size is defined (i.e. an empty set of brackets). They are viewed by the compiler as having an array with one element. For example, Struct_A contains the zero-extent array char arr_a[0], and Struct_B contains the zero-extent array char arr_b[].

struct Struct_A {
   int i;
   char arr_a[0];
} a;

struct Struct_B {
   int i;
   char arr_b[];
} b;

When using zero-sized arrays in unions, they can be declared anywhere within the list of members. When using zero-sized arrays in structures, they must be declared as the last member of the structure. If a structure containing a zero-sized array is declared within another structure, then the member structure must also be declared as the last member of the main structure.

struct Struct_S {
   int i;
   char arr_s[];
};

struct Struct_T {
   float f;
   struct Struct_S;	// error - contains zero-extent array. 
   			// Must be declared as last element
   int k;
} t;

In C++, the zero-sized array must be non-static. In a class definition, the zero-sized array must be the last non-static data member. Members such as functions, static data members, and typedefs are allowed after the zero-sized array. A class containing a zero-sized array cannot be used as a base class.