On Mar 10, 10:23 am, Matt Kowalczyk <
[email protected]> wrote:
[edited to mininal example]
struct foo {
int a1;
int a2;
} foobar[10] = {
[0].a1 = 10,
[2] = {20, 2}
};
Rajesh S R said:
I am not able to understand how the above initialisation is related
with Compound literals ...
It is unrelated to "compound literals". The above is an example of
"designated initializers".
A "compound literal" is a syntactic entity, consisting of two
main parts. The first part looks exactly like a cast: it has
a type-name enclosed in parentheses. The second part looks
exactly like an aggregate initializer: it has values enclosed
in braces. So:
(int) { 2 }
or:
(struct foo) { 9, 7 }
are both examples of compound literals.
A "designated initializer" is also a synactic entity, consisting
of two main parts. The second part is any ordinary initializer
(which may or may not be enclosed in braces as usual); the first
part is the designator, consisting of things like "[2] =" or
".a1 =".
You can combine designated initializers with compound literals,
as in:
(struct foo) { .a2 = 42 }
Here the compound literal contains a designated initializer, so
this makes a "struct foo" object -- whose storage duration depends
on where the compound literal appears in the source -- whose value
is { 0, 42 }, i.e., the a1 member is initialized to 0, and the a2
member is initialized to 42.
Both designated initializers and compound literals are C99-specific,
hence not supported by many C (C89 or C95) compilers. (C89 and
C90 are the same language: C89 here refers to the 1989 ANSI C
standard, X3.159-9899, while C90 refers to the 1990 ISO C standard,
ISO 9899-1990. While the two standards differ, it is mainly in
section numbering; both describe the same language.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.