Hide thumbs Also See for ENTERPRISE LINUX 4:
Table of Contents

Advertisement

148
Chapter 6. Extensions to the C Language Family
The
or
is known as a designator. You can also use a designator (or the obsolete
[
]
.
index
fieldname
colon syntax) when initializing a union, to specify which element of the union should be used. For
example,
union foo { int i; double d; };
union foo f = { .d = 4 };
will convert 4 to a
to store it in the union using the second element. By contrast, casting 4
double
to type
would store it into the union as the integer
, since it is an integer. (Section 6.23
union foo
i
Cast to a Union Type.)
You can combine this technique of naming elements with ordinary C initialization of successive ele-
ments. Each initializer element that does not have a designator applies to the next consecutive element
of the array or structure. For example,
int a[6] = { [1] = v1, v2, [4] = v4 };
is equivalent to
int a[6] = { 0, v1, v2, 0, v4, 0 };
Labeling the elements of an array initializer is especially useful when the indices are characters or
belong to an
type. For example:
enum
int whitespace[256]
= { [' '] = 1, ['\t'] = 1, ['\h'] = 1,
['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
You can also write a series of
and
designators before an
to specify a nested
.
[
]
=
fieldname
index
subobject to initialize; the list is taken relative to the subobject corresponding to the closest surround-
ing brace pair. For example, with the
declaration above:
struct point
struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };
If the same field is initialized multiple times, it will have value from the last initialization. If any
such overridden initialization has side-effect, it is unspecified whether the side-effect happens or not.
Currently, GCC will discard them and issue a warning.

6.22. Case Ranges

You can specify a range of consecutive values in a single
label, like this:
case
case
...
:
low
high

Advertisement

Table of Contents
loading

Table of Contents