Creating an initializer list in C -


how create initializer list in c? have use struct or union?

because have written code apparently has problem how many initialized variables have.

compiling error: number of initializers cannot greater number

struct s {     int a, d, p; };  #define s_initializer { 12, 10, 77 }  struct s s = s_initializer; 

you (typically) provide value each member. if provide less, remaining members zero-initialized (c99, 6.7.8.21). if provide more, compiler warning , additional initializer values discarded.

edit: olaf pointed out (thanks!), better use designated initializers:

#define s_initializer { .a = 12, .d = 10, .p = 77 } 

this way, initializer robust against changes of struct associated with:

struct s {     int d, a, o, p;     //        ^ new value; , d inverted! }; 

is still initialized before, o initialized 0 (c99 6.7.8.19). , if drop p, compile error – @ least gcc.

actually, standard states:

if designator has form
   . identifier
current object (defined below) shall have structure or union type , identifier shall name of member of type.

(c99 6.7.8.7). however, did not find happens if not doing so.

similarly:

no initializer shall attempt provide value object not contained within entity being initialized.

(c99 6.7.8.2), again no word if doing otherwise (providing more elements). discarding additional elements seems legal, not compiling not seem illegal either...


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -