By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,947 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,947 IT Pros & Developers. It's quick & easy.

multiple named initialization of same member

P: n/a
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;
}

Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
William Ahern wrote:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2

P: n/a


William Ahern wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.