Designated Initializers - Red Hat ENTERPRISE LINUX 3 - USING GCC Using Instructions

Using the gnu compiler collection (gcc)
Hide thumbs Also See for ENTERPRISE LINUX 3 - USING GCC:
Table of Contents

Advertisement

134
struct foo {int a; char b[2];} structure;
Here is an example of constructing a
structure = ((struct foo) {x + y, 'a', 0});
This is equivalent to writing the following:
{
struct foo temp = {x + y, 'a', 0};
structure = temp;
}
You can also construct an array. If all the elements of the compound literal are (made up of) simple
constant expressions, suitable for use in initializers of objects of static storage duration, then the
compound literal can be coerced to a pointer to its first element and used in such an initializer, as
shown here:
char **foo = (char *[]) { "x", "y", "z" };
Compound literals for scalar types and union types are is also allowed, but then the compound literal
is equivalent to a cast.
As a GNU extension, GCC allows initialization of objects with static storage duration by compound
literals (which is not possible in ISO C99, because the initializer is not a constant). It is handled as
if the object was initialized only with the bracket enclosed list if compound literal's and object types
match. The initializer list of the compound literal must be constant. If the object being initialized has
array type of unknown size, the size is determined by compound literal size.
static struct foo x = (struct foo) {1, 'a', 'b'};
static int y[] = (int []) {1, 2, 3};
static int z[] = (int [3]) {1};
The above lines are equivalent to the following:
static struct foo x = {1, 'a', 'b'};
static int y[] = {1, 2, 3};
static int z[] = {1, 0, 0};

6.22. Designated Initializers

Standard C89 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 ISO C99 you can give the elements in any order, specifying the array indices or structure field
names they apply to, and GNU C allows this as an extension in C89 mode as well. This extension is
not implemented in GNU C++.
To specify an array index, write
int a[6] = { [4] = 29, [2] = 15 };
is equivalent to
int a[6] = { 0, 0, 15, 0, 29, 0 };
The index values must be constant expressions, even if the array being initialized is automatic.
Chapter 6. Extensions to the C Language Family
with a compound literal:
struct foo
before the element value. For example,
[
] =
index

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the ENTERPRISE LINUX 3 - USING GCC and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

This manual is also suitable for:

Enterprise linux 3

Table of Contents