The members of a structure are
usually processed individually, as separate entity. A structure member can be
accessed by writing
structure_variable.member
Here, structure_variable
refers to the name of a structure-type
variable and member refers to the name of a member within the structure. Again,
dot that separates the variable name from the member name.
In the case of nested
structure (if a structure member is itself a structure), the member within
inner structure is accessed as
structure_variable.member.submember
This will be
clear in nested structure section.
An Example:
Create a structure named student
that has name, roll, marks and remarks as members. Assume appropriate types and
size of member. Write a program using structure to read and display the data
entered by the user.
void main()
{
struct student{ {
char name[20];
int roll;
float marks;
char remark;
};
struct student s;
printf("Enter name:\t");
gets(s.name);
printf("\nEnter roll:\t");
scanf("%d",&s.roll);
printf("\n Enter marks\t");
scanf("%f",&s.marks);
printf("Enter remark p for pass or f for fail\t");
s.remark=getche();
printf("\n\n\nThe Detail Information is\n");
printf("\Name=%s\tRoll=%d\nMarks=%f\tRemark=%c",s.name,s.roll,s.marks,s.remark);
getch();
}
Comments
Post a Comment