Nested Structure
Nested Structure
struct student
{
int roll;
char sname[20];
float per;
struct college c1; // nested of structure
}s1;
void main()
{
//struct student s1={10,"rahul",98.0,101,"psc"};// direct
initialisation
clrscr();
printf("\n enter records");
scanf("%d%s%f%d
%s",&s1.roll,&s1.sname,&s1.per,&s1.c1.cid,&s1.c1.cname);
printf("\n roll\tsname\tper\t\tcid\tcname");
printf("\n%d\t%s\t%f\t%d\t
%s",s1.roll,s1.sname,s1.per,s1.c1.cid,s1.c1.cname);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct college
{
int cid;
char cname[20];
};
struct student
{
int roll;
char sname[20];
float per;
struct college c1; // nested of structure
}s1,*s2;// s2 is pointer object
void main()
{
// struct student s1={10,"rahul",98.0,101,"psc"};// direct
initialisation
clrscr();
printf("\n enter records");
scanf("%d%s%f%d
%s",&s1.roll,&s1.sname,&s1.per,&s1.c1.cid,&s1.c1.cname);
s2=&s1;// assign add of s1 to s2
printf("\n roll\tsname\tper\t\tcid\tcname");
printf("\n%d\t%s\t%f\t%d\t%s",s2->roll,s2->sname,s2->per,s2-
>c1.cid,s2->c1.cname);
getch();
}
ex:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void insert();
void show();
struct student
int roll;
char name[20];
char add[10];
}s1;
void main()
clrscr();
while(k)
clrscr();
scanf("%d",&ch);
if(ch==1)
insert();
}
else if(ch==2)
show();
else if(ch==3)
exit(0);
else
scanf("%d",&k);
getch();
void insert()
scanf("%d%s%s",&s1.roll,s1.name,s1.add);
void show()
printf("\n ROLL\tNAME\tADDRESS");
printf("\n%d\t%s\t%s",s1.roll,s1.name,s1.add);
Structure:-
Struct student
{
Int roll;
Char name[10];
Float fee;
}s1;
roll name fee
name
only recent data resides in the memory block and at
a time only one data a variable have.
Ex:-
#include<conio.h>
#include<stdio.h>
#include<string.h>
union student
{
int roll;
char name[10];
char add[10];
}s1;
void main()
{
clrscr();
printf("\n enter roll");
scanf("%d",&s1.roll);
printf("\n roll=%d",s1.roll);
printf("\n enter name");
scanf("%s",&s1.name);
printf("\n name=%s",s1.name);
printf("\n enter add");
scanf("%s",&s1.add);
printf("\n add=%s",s1.add);
getch();
}