Friday, July 31, 2009

Is it possible to make an array of structures in C?

how do declare it? how does it work?

Is it possible to make an array of structures in C?
Take a look at this,





struct day


{


char month[3];


int day;


int year;


};


void PrintIt( struct day bd );





void main(void)


{


-------%26gt; struct day Birthday[15]; %26lt;----Array of Structures





Birthday[2].month[0]=3D=92J=92;


Birthday[2].month[1]=3D=92a=92;


Birthday[2].month[2]=3D=92n=92;


Birthday[2].day=3D21;


Birthday[2].year=3D1970;





PrintIt(Birthday[2]);





}


void PrintIt( struct day bd )


{


printf (=93\nMonth %c%c%c =94, bd.month[0], bd.month[1] bd.month[2]);


printf (=93Day %d =94, bd.day );


printf (=93Year %d=94, bd.year);


}
Reply:Once you've declared a struct, you can use it like any primitive type. You can have an array of them, linked list of them, etc... Even jagged or multi-dimensional arrays are possible.
Reply:You declare an array of structures the same way you would declare an array of integers or an array of characters. Just remember to include struct when specifying the datatype:





struct MyStruct; //declaration


.


.


.


struct MyStruct arrStruct[SOME_NUM]; //array of structures


No comments:

Post a Comment