Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does initilizing a struct with {0} works? 1

Status
Not open for further replies.

BTCMan

Programmer
Jun 17, 2005
21
BR
Recently I saw a code that initilizes a struct using {0} .My question is if this works for all elements of the struct or just the 1st one. Example:

.H File:

typedef struct
{
UINT32 size;
UINT16 num1;
UINT8 * p_buffer;
} MY_RECORD;

--------------
.C File:

...
MY_RECORD my_var = {0};


 
I believe the safest way to initialize the whole struct is:

memset(&my_var, 0, sizeof(MY_RECORD));
 
> My question is if this works for all elements of the struct or just the 1st one. Example:
It works for all elements of the struct, and all unspecified members of the struct will be initialised with a 0 of the appropriate type (see next comment).

It's also a better way than using memset.
All-bits-zero is not necessarily a NULL pointer, nor floating point 0.0.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top