====== Declaring and initializing an array of structures in C ======
Under certain circumstances, you might need to work with an array of structures. The classic way to do this is to define the structure type, then instantiate and initialize an array of these structures after entering the main function. The C language offers other ways to work on arrays of structures. The following example shows how to build and initialize an array of structures in C in one instruction.
===== Example declaring and initializing an array of structures =====
static struct shape_table_st {
char *Name;
char* Category;
int NumberEdges;
int (*Draw)(int Fgcolor, int Bgcolor);
} shape_table[]={
{"Square", "Polygon", 4, DrawSquare},
{"Triangle", "Polygon", 3, DrawTriangle},
{NULL, NULL, 0, NULL},
};
The construct above declares an array of structures "shape" and initializes it with a Square and a Triangle.
The elements of the array are accessed as follows :
struct shape_table_st Square = shape_table[0];
struct shape_table_st Triangle = shape_table[1];
{{tag>coding}}
~~DISCUSSION~~