Structure in C
Structure in c language is a user defined datatype that allows you to hold different type of elements.Each element of a structure is called a member.
It works like a template in C++ and class in Java. You can have different type of elements in it.
It is widely used to store student information, employee information, product information, book information etc.
Defining structure
The struct keyword is used to define structure. Let's see the syntax to define structure in c.- struct structure_name
- {
- data_type member1;
- data_type member2;
- .
- .
- data_type memeberN;
- };
- struct employee
- { int id;
- char name[50];
- float salary;
- };
Declaring structure variable
We can declare variable for the structure, so that we can access the member of structure easily. There are two ways to declare structure variable:- By struct keyword within main() function
- By declaring variable at the time of defining structure.
Let's see the example to declare structure variable by struct keyword. It should be declared within the main function.
- struct employee
- { int id;
- char name[50];
- float salary;
- };
- struct employee e1, e2;
Let's see another way to declare variable at the time of defining structure.
- struct employee
- { int id;
- char name[50];
- float salary;
- }e1,e2;
Which approach is good
But if no. of variable are not fixed, use 1st approach. It provides you flexibility to declare the structure variable many times.If no. of variables are fixed, use 2nd approach. It saves your code to declare variable in main() fuction.
Accessing members of structure
There are two ways to access structure members:- By . (member or dot operator)
- By -> (structure pointer operator)
- p1.id
C Structure example
Let's see a simple example of structure in C language.- #include <stdio.h>
- #include <string.h>
- struct employee
- { int id;
- char name[50];
- }e1; //declaring e1 variable for structure
- int main( )
- {
- //store first employee information
- e1.id=101;
- strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
- //printing first employee information
- printf( "employee 1 id : %d\n", e1.id);
- printf( "employee 1 name : %s\n", e1.name);
- return 0;
- }
employee 1 id : 101 employee 1 name : Sonoo Jaiswal
- #include <stdio.h>
- #include <string.h>
- struct employee
- { int id;
- char name[50];
- float salary;
- }e1,e2; //declaring e1 and e2 variables for structure
- int main( )
- {
- //store first employee information
- e1.id=101;
- strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
- e1.salary=56000;
- //store second employee information
- e2.id=102;
- strcpy(e2.name, "James Bond");
- e2.salary=126000;
- //printing first employee information
- printf( "employee 1 id : %d\n", e1.id);
- printf( "employee 1 name : %s\n", e1.name);
- printf( "employee 1 salary : %f\n", e1.salary);
- //printing second employee information
- printf( "employee 2 id : %d\n", e2.id);
- printf( "employee 2 name : %s\n", e2.name);
- printf( "employee 2 salary : %f\n", e2.salary);
- return 0;
- }
employee 1 id : 101 employee 1 name : Sonoo Jaiswal employee 1 salary : 56000.000000 employee 2 id : 102 employee 2 name : James Bond employee 2 salary : 126000.000000
Array of Structures in C
There can be array of structures in C programming to store many information of different data types. The array of structures is also known as collection of structures.Let's see an example of structure with array that stores information of 5 students and prints it.
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- struct student{
- int rollno;
- char name[10];
- };
- void main(){
- int i;
- struct student st[5];
- clrscr();
- printf("Enter Records of 5 students");
- for(i=0;i<5;i++){
- printf("\nEnter Rollno:");
- scanf("%d",&st[i].rollno);
- printf("\nEnter Name:");
- scanf("%s",&st[i].name);
- }
- printf("\nStudent Information List:");
- for(i=0;i<5;i++){
- printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
- }
- getch();
- }
Enter Records of 5 students Enter Rollno:1 Enter Name:Sonoo Enter Rollno:2 Enter Name:Ratan Enter Rollno:3 Enter Name:Vimal Enter Rollno:4 Enter Name:James Enter Rollno:5 Enter Name:Sarfraz Student Information List: Rollno:1, Name:Sonoo Rollno:2, Name:Ratan Rollno:3, Name:Vimal Rollno:4, Name:James Rollno:5, Name:Sarfraz
Nested Structure in C
Nested structure in c language can have another structure as a member. There are two ways to define nested structure in c language:- By separate structure
- By Embedded structure
1) Separate structure
We can create 2 structures, but dependent structure should be used inside the main structure as a member. Let's see the code of nested structure.- struct Date
- {
- int dd;
- int mm;
- int yyyy;
- };
- struct Employee
- {
- int id;
- char name[20];
- struct Date doj;
- }emp1;
2) Embedded structure
We can define structure within the structure also. It requires less code than previous way. But it can't be used in many structures.- struct Employee
- {
- int id;
- char name[20];
- struct Date
- {
- int dd;
- int mm;
- int yyyy;
- }doj;
- }emp1;
Accessing Nested Structure
We can access the member of nested structure by Outer_Structure.Nested_Structure.member as given below:- e1.doj.dd
- e1.doj.mm
- e1.doj.yyyy
C Nested Structure example
Let's see a simple example of nested structure in C language.- #include <stdio.h>
- #include <string.h>
- struct Employee
- {
- int id;
- char name[20];
- struct Date
- {
- int dd;
- int mm;
- int yyyy;
- }doj;
- }e1;
- int main( )
- {
- //storing employee information
- e1.id=101;
- strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
- e1.doj.dd=10;
- e1.doj.mm=11;
- e1.doj.yyyy=2014;
- //printing first employee information
- printf( "employee id : %d\n", e1.id);
- printf( "employee name : %s\n", e1.name);
- printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
- return 0;
- }
employee id : 101 employee name : Sonoo Jaiswal employee date of joining (dd/mm/yyyy) : 10/11/2014
No comments:
Post a Comment
Please write your view and suggestion....