c - Kind of sparse initialization for structures, any resources? -


i used initialize structures in way:

struct a = {0}; 

this seems work me, argued ansi c, c89, c99 standard.
couldn't find in documentation.
me that?
here's example works 'cl' (vs express 2008).

#include <stdio.h> struct data {     int a;     int b;     char tab[3]; };   int main(void) {     struct data a;     struct data b = {0};      printf("a.a: %d, a.b: %d, a.tab: %s\n", a.a, a.b, a.tab);     printf("b.a: %d, b.b: %d, b.tab: %s", b.a, b.b, b.tab); }; 
>>>>>output: d:\n\workspace>test.exe  a.a: 4203600, a.b: 451445257, a.tab: ■    b.a: 0, b.b: 0, b.tab: 

this 1 shows initialize first 1, rest 0's.

#include <stdio.h> #include <stdlib.h>  typedef struct {     int a;     int b; } asdf;  asdf = {1};  int main() {     printf("a:%d,b:%d\n",a.a,a.b);     return 0; } 
output: a:1,b:0 

you right, work. relevant section in c99 draft n1256 6.7.8 (initialization):

21. if there fewer initializers in brace-enclosed list there elements or members of aggregate, or fewer characters in string literal used initialize array of known size there elements in array, remainder of aggregate shall initialized implicitly same objects have static storage duration.

objects of static storage duration initialised 0 (paragraph 10 of same section).

the ansi standard shorter, similar in 3.5.7:

if there fewer initializers in list there members of aggregate, remainder of aggregate shall initialized implicitly same objects have static storage duration.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -