Working With Structure In C
Structure is a collection of different or heterogeneous type of data.We know Array is a collection of similar or homogeneous type of data.This purpose of using structure is similar to array.But there are some differences such as:
- Array contain element of same data type but Structure contain element of different data type.
- Array elements are identified by their position but In structure each elements are identified by their unique name.
Let we understand more clearly about structure
Suppose you want to store data about student-name(char) ,student-course(char),student-branch(char),percentage(float) for different datatypes then for this we can use array or structure.Let we analyse what is the advantages and disadvantages of using both the cases separately so that we can understand clearly let we see:
Case - I By Using Array
/*For storing words, a 2-D char array is required.
In this 2-D array, each row will contain a word each.
Hence the rows will denote the index number of the words and
the column number will denote the particular character in that word.*/
#include <stdio.h>
void main()
{
int i;
char sname[3][50],scourse[3][50],sbranch[3][50];
float per[50];
printf("Enter sname scourse sbranch and per:");
for(i=0;i<=2;i++)
{
scanf("%s %s %s %f",&sname[i],&scourse[i],&sbranch[i],&per[i]);
}
printf("Record of sname scourse sbranch and per are:");
for(i=0;i<=2;i++)
{
printf("%s %s %s %f\n",sname[i],scourse[i],sbranch[i],per[i]);
}
getch();
}
Out Put:
But the program using array become more difficult as the number of item increases. So let we try to do same program using structure in case-II
Case - II By Using Structure
#include <stdio.h>
#include <conio.h>
struct student
{
char sname[50];
char scourse[50];
char sbranch[50];
float per;
};
void main()
{
struct student s1,s2,s3;
printf("Enter sname scourse sbranch and per:");
scanf("%s %s %s %f",&s1.sname,&s1.scourse,&s1.sbranch,&s1.per);
scanf("%s %s %s %f",&s2.sname,&s2.scourse,&s2.sbranch,&s2.per);
scanf("%s %s %s %f",&s3.sname,&s3.scourse,&s3.sbranch,&s3.per);
printf("\n%s %s %s %f",s1.sname,s1.scourse,s1.sbranch,s1.per);
printf("\n%s %s %s %f",s2.sname,s2.scourse,s2.sbranch,s2.per);
printf("\n%s %s %s %f",s3.sname,s3.scourse,s3.sbranch,s3.per);
getch();
}
OR
Copy Text
#include <stdio.h>
#include <conio.h>
struct student
{
char sname[50];
char scourse[50];
char sbranch[50];
float per;
};
void main()
{
struct student st[3]; // Array of Structure
int i;
printf("Enter sname scourse sbranch and per:");
for(i=0;i<3;i++)
{
scanf("%s %s %s %f",&st[i].sname,&st[i].scourse,&st[i].sbranch,&st[i].per);
}
for(i=0;i<3;i++)
{
printf("\n%s %s %s %f",st[i].sname,st[i].scourse,st[i].sbranch,st[i].per);
}
getch();
}
Out Put:
Nesting of Structure:
#include <stdio.h>
#include <conio.h>
struct address
{
char phone[50];
char city[50];
int pin;
};
struct student
{
char sname[50];
char scourse[50];
char sbranch[50];
struct address a;
};
void main()
{
struct student st={"Shailu","Btech","CSE","9999999999","Bhopal","464646"};
printf("Display Student Details:");
{
printf("\n%s %s %s %s %s %d",st.sname,st.scourse,st.sbranch,st.a.phone,st.a.city,st.a.pin);
}
getch();
}
Out Put:
How to pass Structure variables in function:
#include <stdio.h>
#include <conio.h>
struct student
{
char sname[50];
char scourse[50];
char sbranch[50];
};
void main()
{
struct student st={"Shailu","Btech","CSE"};
display(st.sname,st.scourse,st.sbranch); // call function
getch();
}
//Display Function
void display(char *sn,char *sc,char *sb)
{
printf("Display Student Details:");
{
printf("\n%s %s %s",sn,sc,sb);
}
}
Out Put:
Assignments:
- Write a program to store Name,Rollnumber,Sex,height and weight of 5 students using structure.
- Write a program to store name,rollno and date of birth of 5 student using structure.Use one saperate structure for date of birth contain day,month,year.
No comments:
Post a Comment