struct initializer { 0 }

M

M Kumar

Suppose I have a struct:

struct ncb
{
int a, b, c, d;
long e, f, g, h;
unsigned long i, j, k, l;
unsigned char buf[20];
};

can I use

struct ncb n1 = { 0 };

to set it to zero? Is this valid/legal C?

TIA,
K
 
R

rihad

Suppose I have a struct:

struct ncb
{
int a, b, c, d;
long e, f, g, h;
unsigned long i, j, k, l;
unsigned char buf[20];
};

can I use

struct ncb n1 = { 0 };

to set it to zero? Is this valid/legal C?

Sure! It's much more C than the dreaded memset(&n1, 0, sizeof ncb); used so
frequently in Windows programming.
 
I

Ivan Vecerina

M Kumar said:
Suppose I have a struct:

struct ncb
{
int a, b, c, d;
long e, f, g, h;
unsigned long i, j, k, l;
unsigned char buf[20];
};

can I use

struct ncb n1 = { 0 };

to set it to zero? Is this valid/legal C?

Yes. All members will be set to zero (or NULL).

hth-Ivan
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top