Question from C faq in comp.lang.c

S

Shastri

Hi all,

Can I get a clear idea of what designated initializers in the below
question mean??
Also is it applicable for structures too??


Can I initialize unions?
A: In the original ANSI C, only the first-named member; in C99,
using "designated initializers", yes, any member.


Thanks
Shastri
 
M

Mark McIntyre

Hi all,

Can I get a clear idea of what designated initializers in the below
question mean??
Also is it applicable for structures too??


Can I initialize unions?
A: In the original ANSI C, only the first-named member; in C99,
using "designated initializers", yes, any member.


struct foo{
int x;
double y;
char* z;
}

struct foo = { .x=1; .z="boo";};

Mark McIntyre
 
C

Chris Torek

On 1 Feb 2006 15:18:16 -0800, in comp.lang.c , "Shastri"
Can I get a clear idea of what "designated initializers" [are]?

struct foo{
int x;
double y;
char* z;
}

struct foo = { .x=1; .z="boo";};

This syntax is a little bit wrong -- you mean:

struct foo some_var = { .x = 1, .z = "boo" };

It might also be worth including an array example, and/or a union:

union u {
int i;
double d;
} a[5] = {
[4].d = 3.141592653589793238462643383279502884,
[0].i = 42,
/* the rest will have .i initialized to 0 */
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top