struct initialization

L

Leo Havmøller

Hi,

A struct:

typedef struct
{
int a;
char b;
long c;
} testtype;

A fairly typical way of initializing all field to zero is:

testtype s1 = { 0 };

But i recently came across the following:

testtype s2 = { };

I.e. nothing between the braces.
What does the standards say about that?
Are all fields zeroed like in the first example?

Leo Havmøller.
 
B

Ben Bacarisse

Leo Havmøller said:
A struct:

typedef struct
{
int a;
char b;
long c;
} testtype;

A fairly typical way of initializing all field to zero is:

testtype s1 = { 0 };

But i recently came across the following:

testtype s2 = { };

I.e. nothing between the braces.
What does the standards say about that?

It's a syntax error.
Are all fields zeroed like in the first example?

If it is accepted as an extension, I'd expect that to be the case, but
it is not ISO C.
 
I

Igmar Palsenberg

testtype s2 = { };

I.e. nothing between the braces.
What does the standards say about that?

That it isn't allowed.
Are all fields zeroed like in the first example?

If your compiler supports it, that is most likely the case.



Igmar
 
L

Leo Havmøller

Thank you very much for the responses, everyone.
I saw it in some code that AFAIK was written this year.
It is currently compiled with GCC 4.3.3, but it will also be used with other
compilers, so i think i will locate it again and fix it.

Leo Havmøller.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top