Struct initialisation

A

Allan Bruce

I have made a struct which has several fields and I initialise like this:

MyStruct foo = {a, b, c, d, e};

I just added another field to the struct at the end, and I thought I would
have to add this new field to all of the initialisers but I dont, is this
defined behaviour?
Thanks
Allan



Here is my code:


/* define struct */
typedef struct
{
HWND xiParentWnd;
const char *xiCaption;
const char *xiTitle;
DWORD xiFlags;
DWORD xiNotifyMsg;
bool AutoDelete;
} AsyncMessageBoxInfo;


/* an example initialising the strcut */
AsyncMessageBoxInfo lAMBI = {NULL, "User is no longer logged in",
mRemoteUser, MB_OK | WS_EX_TOPMOST, NULL};
 
V

Vijay Kumar R Zanvar

Allan Bruce said:
I have made a struct which has several fields and I initialise like this:

MyStruct foo = {a, b, c, d, e};

I just added another field to the struct at the end, and I thought I would
have to add this new field to all of the initialisers but I dont, is this
defined behaviour?
Thanks
Allan



Here is my code:


/* define struct */
typedef struct
{
HWND xiParentWnd;
const char *xiCaption;
const char *xiTitle;
DWORD xiFlags;
DWORD xiNotifyMsg;
bool AutoDelete;
} AsyncMessageBoxInfo;


/* an example initialising the strcut */
AsyncMessageBoxInfo lAMBI = {NULL, "User is no longer logged in",
mRemoteUser, MB_OK | WS_EX_TOPMOST, NULL};

Perfectly defined behaviour. AutoDelete would be set to zero.
I think `bool' is of an integral type.

Vijay
 
L

Leor Zolman

I have made a struct which has several fields and I initialise like this:

MyStruct foo = {a, b, c, d, e};

I just added another field to the struct at the end, and I thought I would
have to add this new field to all of the initialisers but I dont, is this
defined behaviour?
Thanks
Allan

Yes. 6.7.8/19 of the Standard says:

"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."

This applies to both arrays and structs; it seems to get discussed more
often in the context of array initialization, however (in fact, I
misinterpreted it just the other day...)
-leor
 

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
474,142
Messages
2,570,820
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top