0% found this document useful (0 votes)
6 views5 pages

3.nested Structure

Uploaded by

Manoj Jakhale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

3.nested Structure

Uploaded by

Manoj Jakhale
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

 Nested structure/ Nesting of Structure

A structure within structure is known as “Nested structure”.


The syntax is
struct structure_name1
{
data type member1;
data type member2;
.
data type membern;
Outer
struct structure_name2
{
data type member1;
inner
data type member2;
.
data type membern;
} inner structure_variable_name;
} outer structure_variable_name;

**Example
#include<stdio.h>
struct student
{
int rno;
char name[20];
struct mark
{
int marks;
}p;
}q;
int main()
{
printf(“enter rno”);
scanf(“%d”,&[Link]);
printf(“enter name”);
scanf(“%s”,[Link]);
printf(“enter marks”);
scanf(“%d”,&[Link]);
printf(“\n rno is%d”,[Link]);
printf(“\n name is%s”,[Link]);
printf(“ marks is %d”,[Link]);
return 0;
}
Here,
The structure student consists of structure mark, which itself
is a structure with two members.
The structure student is known as “Outer structure” while
structure mark is known as “inner structure”.
The members which are inside the outer structure can be
accessed by [Link] and [Link].
The members which are inside the inner structure can be
accessed by [Link].
 Union
Union is similar to structure. Union is also a user defined
data type used to store similar or different data type under a unique
name.

** definition of union
“A union is a user defined data type which contains similar
or different data types.

** How to define/declare union


The syntax to define/declare a union is as follows
union union_name
{
data type member1;
data type member2;
.
.
data type membern;
};
Here,
‘union’ keyword declare a union that store multiple
variables(members) of different data types. These variables
(elements) are known as “union elements/members of union”.
Each member may have same or different data type. The name of the
union is known as “union_name”.
union student
{
int rno;
char name[20];
};
Here,
union keyword declare a union student with members rno and
name.
** declaration of union_variable
union_variable can be declare by using 2 methods,
1) outside main()
Syntax union union_name
{
// members of union
}union_variable_name1… union_variable_namen;
e.g union student
{
int rno;
char name[20];
} p; <----- union_variable_name

2) inside main()
Syntax
union union_variable_name1… union_variable_namen;
e.g union student p;
**Example
#include<stdio.h>
union student
{
int rno;
char name[20];
};
int main()
{
union student p; // union_variable_name
printf(“enter rno”);
scanf(“%d”,&[Link]);
printf(“\n rno is %d”,[Link]);
printf(“enter name”);
scanf(“%s”,[Link]);
printf(“\n name is %d”,[Link])
return 0;
}
**Accessing union members/elements
We can access structure elements by using member operator
‘.’, which is also called as “dot operator or period operator”.
In the above example we have read rno and name by using
scanf () and write (display) rno and name by using printf() with
dot(.) operator.

You might also like