Whole document tree
4.20 Labeled Elements in InitializersStandard C requires the elements of an initializer to appear in a fixed order, the same as the order of the elements in the array or structure being initialized. In GNU C you can give the elements in any order, specifying the array indices or structure field names they apply to. This extension is not implemented in GNU C++. To specify an array index, write `[index]' or `[index] =' before the element value. For example,
is equivalent to
The index values must be constant expressions, even if the array being initialized is automatic. To initialize a range of elements to the same value, write `[first ... last] = value'. For example,
Note that the length of the array is the highest value specified plus one. In a structure initializer, specify the name of a field to initialize with `fieldname:' before the element value. For example, given the following structure,
the following initialization
is equivalent to
Another syntax which has the same meaning is `.fieldname ='., as shown here:
You can also use an element label (with either the colon syntax or the period-equal syntax) when initializing a union, to specify which element of the union should be used. For example,
will convert 4 to a You can combine this technique of naming elements with ordinary C initialization of successive elements. Each initializer element that does not have a label applies to the next consecutive element of the array or structure. For example,
is equivalent to
Labeling the elements of an array initializer is especially useful
when the indices are characters or belong to an
This document was generated by root on January, 30 2002 using texi2html |