Structure is a user defined data type which is used to store heterogeneous data inder unique name. Keyword 'struct' is used to declare structure.
The variable which are declared inside the structure as called as 'members of structure'.
struct structure_name { < data_type > element 1; < data_type > element 2; -------- -------- < data_type > element n; }struct_var; Example: struct emp_info { char emp_id[10]; char name[100]; float sal; }emp;
Note:
Instances of structure can be created in two ways as,
struct emp_info { char emp_id[10]; char name[100]; float sal; }emp;
struct emp_info { char emp_id[10]; char name[100]; float sal; } struct emp_info emp;
In above example, emp_info is a simple structure which consists of structure members as Employee ID(emp_id), Emlpoyee Name(name) , Employee salary(sal).
Structure members can be accessed using member operator '.'. It is also called as 'dot operator' or 'period operator'
#include < stdio.h > #include < conio.h > struct comp_info { char name[10]; char addr[100]; }info; void main() { clrscr(); printf("\n Enter company Name:"); gets(info.name); printf("\n Enter Address:"); gets(info.addr); printf("\n\nCompany Name: %s", info.name); printf("\n\nAddress: %s:", info.addr); getch(); } output: Enter Company Name: TechnoExam, Technowell Web Solutions Enter Address: Sangli, Maharashtra, India Company Name: TechnoExam, Technowell Web Solutions Address: Sangli, Maharashtra, India
Sometimes, it is necessary to use structure members with array.
#include < stdio.h > #include < conio.h > struct result { int rno, marks[5]; char name[100]; }res; void main() { int i, total; clrscr(); printf("\n Enter Roll Number:"); scanf("%d", &res.rno); printf("\n Enter 3 subject marks:"); for(i=0; i<3; i++) { scanf("%d", &res.marks[i]); total = total + res.marks[i]; } printf("\n\n\t Roll Number: %d", res.rno); printf("\n\n\t Marks are:"); for(i=0; i<3; i++) { printf("%d", res.marks[i]); } printf("\n\n\t Total is : %d", total); getch(); } Output: Enter Roll Number: 1 Enter three subject marks: 63 66 68 Roll Number: 1 Marks are: 63 66 68 Total is : 197
We can create structures with array for ease of operations in case of getting multiple same fileds.
#include < stdio.h > #include < conio.h > struct emp_info { int emp_id; char name[50]; }emp[2]; void main() { int i; clrscr(); for(i=0; i<2; i++) { printf("\n\n\tEnter Employee ID:"); scanf("%d", &emp[i].emp_id); printf("\n\n\tEmployee Name:"); scanf("%d", &emp[i].name); } for(i=0, i<2; i++) { printf("\n\tEmployee ID:%d", emp[i].emp_id); printf("\n\tEmployee Name:%s", emp[i].name); } getch(); } Output: Enter Employee ID: 1 Employee Name: ABC Enter Employee ID: 2 Employee Name: XYZ Employee ID: 1 Employee Name: ABC Employee ID: 2 Employee Name: XYZ
Structures can be used as structures within structures. It is also calles 'nesting of structures'
Syntax
struct structure_name { < data_type > element 1; < data_type > element 2; -------- -------- < data_type > element n; struct structure_name2 { < data_type > element 1; < data_type > element 2; -------- -------- < data_type > element n; }inner_struct_variable; }outoer_struct_variable; Example: struct stud_res { int rno; char name[50]; char std[10]; struct stud_subject { char subject_name[30]; int marks; }subject; }result;
In above example, the structure stud_res consists of stud_subject which itself is a structure with two members. Structure stud_res is called as 'outer structure' while stud_subject is called as 'inner structure'. The members which are inside the inner structure can be accessed as follow:
result.subject.subject_name result.subject.marks #include< stdio.h > #include< conio.h > struct stud_res { int rno; char std[10]; struct stud_marks; { char subj_name[30]; int subj_mark; }marks; }result; void main() { clrscr(); printf("\n\t Enter Roll Number: "); scanf("%d", &result.rno); printf("\n\t Enter Standard: "); scanf("%d", &result.std); printf("\n\t Enter subject code: "); scanf("%d", &result.marks.subj_name); printf("\n\t Enter Marks: "); scanf("%d", &result.marks.subj_mark); printf("\n\n\t Roll Number: ",result.rno); printf("\n\n\t Standard: ",result.std); printf("\n\n\t Subject Code: ",result.marks.subj_name); printf("\n\n\t Marks: ",result.marks.subj_mark); getch(); } Output: Enter Roll Number: 1 Enter Standard: MCA(Sci)-I Enter Subject code: SUB001 Enter Marks: 63 Roll Number: 1 Standard: MCA(Sci)-I Subject Code: SUB001 Marks: 63