0% found this document useful (0 votes)
375 views

Nested Structure

1. Nested structures in C allow one structure to be declared as a member of another structure, allowing structures to be nested within each other. 2. The document provides an example of a nested structure with a student structure containing a college structure as one of its members. 3. Structures can be accessed using dot (.) operator when declaring a normal structure variable, but arrow (->) operator must be used to access members of a structure pointer variable.

Uploaded by

Kushagra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
375 views

Nested Structure

1. Nested structures in C allow one structure to be declared as a member of another structure, allowing structures to be nested within each other. 2. The document provides an example of a nested structure with a student structure containing a college structure as one of its members. 3. Structures can be accessed using dot (.) operator when declaring a normal structure variable, but arrow (->) operator must be used to access members of a structure pointer variable.

Uploaded by

Kushagra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Nested structure:-

Nesting of structure , is also permitting in c, where one structure


has another structure as its members variable.
syntax:-
struct s1
{
Data members1;
Struct s2
{
Data members2;
}p;
}object;
Notes:-
1. Nested structure in c is nothing but structure within
structure. one structure can be declared inside other
structure as we declare members of the structrure.
2. The structure variable can be a normal structure variable or
a pointer variable to access the data.
#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;
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();
}

Structure within structure (nested) using pointer variable:-


1. When we want to access the data member of any structure
then we use dot (.) operator but if the object or structure
variable is pointer object then we should use arrow
operator(->) instead of dot (.) operator.

#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();
}

Wap to create a console based application of bank


management system.which provides the functions or
methods as:-
1. Create account
2. Display account_detail
3. Deposit
4. Withdraw
5. logout

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()

int ch,k=1;// 1 means true

clrscr();

while(k)

clrscr();

printf("\n \\\\\\\\\WELCOME TO PATNA SCI COLLEGE///////////");

printf("\n PRESS 1 FOR INSERT \n PRESS 2 FOR SHOW\nPRESS 3 FOR EXIT");

scanf("%d",&ch);

if(ch==1)

insert();

}
else if(ch==2)

show();

else if(ch==3)

exit(0);

else

printf("\n invalid choice");

printf("\n wanna continue? \npress 0 for NO and 1 for YES");

scanf("%d",&k);

getch();

void insert()

printf("\n insert roll name and address of student");

scanf("%d%s%s",&s1.roll,s1.name,s1.add);

printf("\n successfully inserted data");

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

2 bytes 10 bytes 4 bytes= 16 bytes


Union:-
It applied common memory allocation:-
roll 10 bytes
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();
}

You might also like