c - Single line Initialization of Array of Structures -


i trying declare , initialize array of structure in 1 line. looks not supported in arrays. in below code, mystructarray[3] gives error whereas mystructarray2[3] works ok. why initialization in 1 line not allowed (i know not practice do, still want know reason if any.)

typedef struct mystruct {        int structmemint;        char structmemchar; }mystruct;  int main (void) {      struct mystruct mystructarray[3] = {2,'a',5,'b',7,'c'};       struct mystruct mystructarray2[3] = {          {2,'a'},          {5,'b'},          {7,'c'},      }; } 

updated:

the point here 1 line initialization without separate braces each array member vs braces. means why not work {2,'a',5,'b',7,'c'} , why must include braces {{2,'a'},{5,'b'},{7,'c'}}?

i comparing behavior multi dimensional array wherein need not separate each row data braces. doubt started because theory , problems of programming c byron gottfried (schaum outline series) explains in example 9.16 (whatever edition have) in multi dimensional array section below. means book wrong in wrong far 2 dimensional array initialization concerned.

int values[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}; 

enter image description here

also 1 more book (i not have book details , got web) says similar way (attached second picture)

enter image description here

because declare structure type array. each location of array contain 2 type data 1 integer , char. can access each location way

mystructarray2[0].structmemint=2; mystructarray2[0].structmemchar='a'; mystructarray2[1].structmemint=5; mystructarray2[1].structmemchar='b'; mystructarray2[2].structmemint=7; mystructarray2[2].structmemchar='c'; 

or

struct mystruct mystructarray2[3] = {          {2,'a'}, //  location 0          {5,'b'}, //  location 1          {7,'c'}, //  location 2      }; 

but

struct mystruct mystructarray[3] = {2,'a',5,'b',7,'c'}; 

this not correct . hear try insert 6 items not exist in structure declaration.

i think got point.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -