multiple named initialization of same member

W

William Ahern

Is the following legal, and if so is the behavior specified?

struct s {
int a;
const char *b;
};

int main(void) {
struct s test = {
.a = 12,
.b = "",
.a = 16,
};

return 0;
}
 
E

Eric Sosman

William said:
Is the following legal, and if so is the behavior specified?

struct s {
int a;
const char *b;
};

int main(void) {
struct s test = {
.a = 12,
.b = "",
.a = 16,
};

return 0;
}

Yes (assuming C99) and yes. 6.7.8/19:

"The initialization shall occur in initializer list
order, each initializer provided for a particular
subobject overriding any previously listed initializer
for the same subobject [...]"

So your initialization produces test.a == 16, test.b pointing
to an empty string.
 
M

Michael Mair

William said:
Is the following legal, and if so is the behavior specified?

struct s {
int a;
const char *b;
};

int main(void) {
struct s test = {
.a = 12,
.b = "",
.a = 16,
};

return 0;
}
6.7.8 #19 IMO covers this:
,-
| The initialization shall occur in initializer list order, each
| initializer provided for a particular subobject overriding any
| previously listed initializer for the same subobject; all
| subobjects that are not initialized explicitly shall be initialized
| implicitly the same as objects that have static storage duration.
`-

Cheers
Michael
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top