Michael said:
No, it need not. {0} is always a valid initializer for an object
of any complete type, or an array of unknown size. (Obviously it's
not valid for objects of incomplete type, namely incomplete structs
and unions and variable-length arrays.)
For scalar objects, {0} follows from ISO 9899-1999 6.7.8 #11. For
aggregate and union objects, {0} follows from the same section, #13,
#16, #17, and #19-#22. See also (non-normative) footnote 127.
Chapter and verse if you believe otherwise, please.
You could be right since I have only seen the behavior of gcc
in such a case. As you say, probably it is very valid C. I double
checked, it is just that gcc is emitting different warnings. It does
produce correct runtime behavior though.
$>cat struct_init.c
#include <stdio.h>
#include <stdlib.h>
typedef struct foo_phy_st_ {
int age;
int ht;
int wt;
} foo_phy_st;
typedef struct foo_edu_st_ {
int years;
} foo_edu_st;
typedef struct foo_person_st_ {
foo_phy_st phy;
foo_edu_st edu;
} foo_person_st;
int main (void)
{
#ifdef MULTI_BRACES
foo_person_st a = {{0}};
#else
foo_person_st a = {0};
#endif
printf("%d %d\n", a.phy.age, a.edu.years);
return 0;
}
$> gcc -Wall -W -ansi -pedantic struct_init.c
struct_init.c: In function `main':
struct_init.c:25: warning: missing braces around initializer
struct_init.c:25: warning: (near initialization for `a.phy')
struct_init.c:25: warning: missing initializer
struct_init.c:25: warning: (near initialization for `a.phy.ht')
struct_init.c:25: warning: missing initializer
struct_init.c:25: warning: (near initialization for `a.edu')
$> gcc -Wall -W -ansi -pedantic -DMULTI_BRACES struct_init.c
struct_init.c: In function `main':
struct_init.c:23: warning: missing initializer
struct_init.c:23: warning: (near initialization for `a.phy.ht')
struct_init.c:23: warning: missing initializer
struct_init.c:23: warning: (near initialization for `a.edu')
$> gcc -ansi -pedantic struct_init.c
struct_init.c: In function `main':
struct_init.c:25: warning: missing braces around initializer
struct_init.c:25: warning: (near initialization for `a.phy')
struct_init.c:25: warning: missing initializer
struct_init.c:25: warning: (near initialization for `a.phy.ht')
struct_init.c:25: warning: missing initializer
struct_init.c:25: warning: (near initialization for `a.edu')
$> gcc struct_init.c
struct_init.c: In function `main':
struct_init.c:25: warning: missing braces around initializer
struct_init.c:25: warning: (near initialization for `a.phy')
struct_init.c:25: warning: missing initializer
struct_init.c:25: warning: (near initialization for `a.phy.ht')
struct_init.c:25: warning: missing initializer
struct_init.c:25: warning: (near initialization for `a.edu')
$> gcc -Wall -W -ansi -std=c99 -pedantic struct_init.c
struct_init.c: In function `main':
struct_init.c:25: warning: missing braces around initializer
struct_init.c:25: warning: (near initialization for `a.phy')
struct_init.c:25: warning: missing initializer
struct_init.c:25: warning: (near initialization for `a.phy.ht')
struct_init.c:25: warning: missing initializer
struct_init.c:25: warning: (near initialization for `a.edu')
$> gcc --version
gcc (GCC) 3.3.3 (Yellow Dog Linux 3.3.3-16.ydl.8)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
Karthik