Declaring Structure type Array in C Language
Structure type array can be created just like array of basic data types is created.
Syntax:
struct <struct_name><Arr_Name>[Size];
Here, struct is the keyword which specifies that a structure type variable is being initialized.
struct_name referes to the name of structure of which we are going to create a variable.
Arr_Name refers to the name of structure type array.
Size refers to the maximum number of values that can be stored in structure type array.
Example:
struct emp e[20];
In this example, array with name e of structure emp has been declared with size 20.
Program of structure type array.
#include<string.h> #include<stdio.h> struct emp { int empid; char ename[20]; }; int main() { struct emp e1[3]; for(i=0;i<3;i++) { printf("\nEnter employee id="); scanf("%d",&e1[i].empid); printf("\nEnter employee name="); fflush(stdin); gets(e1[i].ename); } for(i=0;i<3;i++) { printf("\nEmployee id = %d",e1[i].empid); printf("\nEmployee Name = %s",e1[i].ename); } return(0); }
Output
Enter employee id =101 Enter employee name=amitEnter employee id =102 Enter employee name=sumitEnter employee id =103 Enter employee name=Rahul Employee id = 101 Employee Name = amit Employee id = 102 Employee Name =sumit Employee id = 103 Employee Name =Rahul