Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > struct initialization

Reply
Thread Tools

struct initialization

 
 
Leo Havmøller
Guest
Posts: n/a
 
      08-19-2009
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.

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-19-2009
Leo Havmøller <> writes:

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

--
Ben.
 
Reply With Quote
 
 
 
 
Igmar Palsenberg
Guest
Posts: n/a
 
      08-19-2009

> 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
 
Reply With Quote
 
Leo Havmøller
Guest
Posts: n/a
 
      08-20-2009
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
static struct initialization in a Class:: -- not my struct christian.bongiorno@gmail.com C++ 2 09-20-2006 06:53 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57