Structure in c
Structure is a group of similar and dissimilar data types. Structure is user defined data type. To create a structure, we use struct keyword. Structure starts with a delimiters ({) and close with a delimiter(}). After closing the delimiters, we place a semicolon(;). To access the elements of the structures, we create a structure variable/ structure field. With the help of structure variable we use the dot operator and a specific element name, which we want to access.
As shown in the syntax below: Syntax
struct<structure name>
{
<data type> <variable>;
<data type><variabie>;
};
Example
struct student
{
char name[20];
int roll, marks ;
};
In this example student is structure name and in this structure we declare two different data types, i.e. char & int.
Example of structure
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int roll,marks;
};
void main()
{
struct student s; //s is structure variable
clrscr();
printf("enter student name");
scanf("%s",&s.name);
printf("enter student roll no");
scanf("%d",&s.roll);
printf("enter student marks");
scanf("%d",&s.marks);
printf("\n your name=%s",s.name);
printf("\n your roll=%d' s.roll);
printf("\n your marks=%d",s.marks);
getch();
}
Initialization of structure
Initialization of structure means to give the initial values to the structure elements in place of giving the values at run time.
As shown in the example given below
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int roll,marks;
};
void main()
{
struct student s={"Harsh", 1,89}; // initialization of structure //variables
clrscr();
printf("\n Student name=%s",s.name);
printf("\n Student roll=%d", s.roll);
printf(\n Student marks=%d", s.marks);
getch();
}
Memory representation in structure: - We can represent the memory allocation of structure as given below. In it we can try to show the memory presentation of structure.
struct student
{
char name[20]:;
int roll, marks;
}
name --> 20 bytes
roll ----> 2 bytes
marks ----> 2 bytes
As shown above it allocates 24 bytes memory for structure variables, 20 bytes memory for name, 2 bytes for roll number and 2 bytes for marks. Total memory is 24 bytes.
Structure within array:- We can also declare an array variable inside the structure. Structure and array both represents the group of elements. As shown in the example
struct emp
{
char name[30];
char address[80];// array
};
Dot Operator:- In structure we can use dot operator to access the elements of the structure. We can use structure variable with dot operator and then given the element name, which we want to access.
As shown in the example
struct student s;
s.name="HARSH":
s.marks=89;
Here s is the structure variable and name, marks are the elements of structure which we are accessing using dot operator. Structure Array We can also create an array of the structure for storing the details of more than one student.
struct student s[5];
Here s is the name of the structure array.The elements of this array are
s[0],s[1],s[2],s[3],s[4].
Write a program to enter the details of five students: their name, roll no and marks using structure.
# include<stdio.h>
# include<conio.h>
struct student
{
char name [20];
int roll,marks;
};
void main()
{
int i;
struct student s[5];
clrscr();
for(i=0;i<5;i++)
{
printf("\n enter your name “);
scanf("%s",&s[i].name);
printf("\n enter roll no");
scanf("%d",&s[i].roll);
printf("\n enter your marks");
scanf("%d",&s[i].marks);
}
for(i=0;i<5;i++)
{
printf("\n your name=%s",s[i].name);
printf("\n your roll no=%d",s[i].roll);
printf("\nyour marks=%d",s[i].marks);
}
getch();
}
Nested structure
Nested structure means a structure inside a structure. It is possible to have one structure as a member of another structure. In nested structure the first structure is called the outer structure and second structure which Inside the structure is known as Inner structure.
#include<stdio.h>
#include<conio.h>
struct emp
{
char name[20];
struct dep
{
int salary;
}d;
};
void main()
{
struct emp e; //e is structure variable
clrscr();
printf("enter Employ name");
scanf("%s",&e.name);printf("enter salary");
scanf("%d",&e.d.salary);
printf("\n Your name =%s",e.name);
printf("\n Your salary =%d",e.d.salary);
getch();
}
Passing structure as argument into funtion :- We can also structure as argument into function. Same as variable and array.As shown in the example . In this program , we write a function show(). It accept as its arguments.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int roll,marks;
}; void show(struct student s); void show(struct student s){
printf("\n Student name=%s",s.name);
printf("in Student roll=%d", s.roll);
printf(\n Student marks=%d", s.marks);
} void main()
{
struct student s={"Harsh", 1,89}; // initialization of structure //variables
clrscr();
show(s);
getch();
}
Return Structure from function , a simple program
#include<stdio.h>#include<conio.h>struct abc{
int a;};abc show();void main(){
clrscr(); abc obj; obj=show(); printf("%d",obj.a); getch();}abc show(){ abc obj1; obj1.a=17; return obj1;}
Return Structure from function and pass structure to function, a simple program
#include<stdio.h>
#include<conio.h>
struct abc
{
int a;
};
abc show(abc);
void main()
{
clrscr();
abc obj,obj1;
obj.a=16;
obj1=show(obj);
printf("%d",obj1.a);
getch();
}
abc show(abc obj2)
{
obj2.a=obj2.a+13;
return obj2;
}
Example of Program Function inside structure in c
We can define function inside the structure in c language, but function will be public , we can call function from anywhere in the program. All data and function inside the structure will be public, It has no privacy, this problem has solved in c++, that is why private concept has come in c++
#include<stdio.h>#include<conio.h>struct student{ int roll,marks; char name[20]; void show() { printf("enter student name"); scanf("%s",&name); printf("\nenter student roll"); scanf("%d",&roll); printf("\nenter student marks"); scanf("%d",&marks); printf("\nenter student nam=%s",name); printf("\nenter student roll=%d",roll); printf("\nenter student marks=%d",marks); }};void main(){ clrscr(); struct strudent s; s.show(); getch();
}
Simple Example of program Sorting Structure in C Language
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20];
int roll,marks;
};
void main()
{
struct student s[3];
int i,j,m;
char n[20];
clrscr();
for(i=0;i<3;i++)
{
printf("\n enter student name");
scanf("%s",&s[i].name);
printf("\n enter student roll no");
scanf("%d",&s[i].roll);
printf("\n enter student marks");
scanf("%d",&s[i].marks);
}
for(i=0;i<3;i++)
{
for(j=i+1;j<3;j++)
{
if(s[i].roll>s[j].roll)
{
m=s[i].roll;
s[i].roll=s[j].roll;
s[j].roll=m;
m=s[i].marks;
s[i].marks=s[j].marks;
s[j].marks=m;
strcpy(n,s[i].name);
strcpy(s[i].name,s[j].name);
strcpy(s[i].name,n);
}
}
}
for(i=1;i<3;i++)
{
printf("\n %s",s[i].name);
printf("\t %d",s[i].roll);
printf("\t %d",s[i].marks);
}
getch();
}
To check the size of the structure
To check the size of the structure, we can use sizeof() operator. It tells us how much memory is occupied by the structure elements.
As shown in the example below:
#include<stdio.h>
#include<conio.h>
struct emp
{
char name[15];
int age;
float salary;
};
void main()
{
struct emp e;
clrscr();
printf("the size of the structure=%d",sizeof(e));
getch();
}Difference between Structure And Array
Structure | Array |
Structure is group of similar and dissimilar elements | Array is group of elements of similar types. |
Structure start with struct keyword | It does not stary with any keyword but is uses subscript for creating array. |
We can use dot operator to access the elements of the structure. | We can use index of the array to access the element of the space array. |
Structure has declaration and definition. | It has only declaration. |
Example struct stu { char name[20]; int roll; }; | Example int a[10]; |
0 comments:
Post a Comment