Structure and Union
Introduction of structure,
Ø Structure is
a user-defined datatype in C language which allows us to combine data
of different types together.
- The keyword struct
is used to define a structure.
- The
collection of variables inside the structure are called as members of structure
- The size of
structure can be calculated as the total size of all members used in the
structure.
- To access
the members of structure dot ( . ) operator is used, if we are working with
pointer structure then points to (->) operator is used.
Declaration & initialization.
The general syntax of structure is
struct name_of_structure
{
data_type variableName1 /
MemberName1
data_type variableName2 /
MemberName2
data_type variableName3 /
MemberName3
.
.
.
data_type variableNamen /
MemberNamen
};
Example:
struct Student
{
int sno;
char sname[20];
float avg;
};
In
above example struct Student is a structure with sno, sname and avg as members
of that structure.
Creating Variable of Structure:
To
create variable of structure following statement is used
struct
Student s1;
Initializing the members of structure:
The structure members can be initialize in
two different ways
1) struct Student s1 = {12,”Rajesh”,88.9};
2) struct Student s1;
s1.sno = 90;
s1.sname=”Rajesh”;
s1.avg=88.9;
in
above example s1 is a variable / instance of structure student. The members of
structure are accessed using dot ( . ) operator.
Programe:
#include<stdio.h>
#include<conio.h>
struct Student
{
int
rno;
char sname[20];
};
void main()
{
struct Student s1;
clrscr();
printf(“Enter Roll number of Student “);
scanf(“%d”, &s1.rno);
printf(“Enter Name of Student “);
scanf(“%s”, &s1.sname);
printf(“Roll Number = %d”,s1.rno);
printf(“\n Name = %s”, s1.sname);
getch();
}
Arrays of Structures:
We can also
declare an array of structure variables in which each element
of the array will
represent a structure variable.
Example
: struct Student s1[5];
The program
below defines an array s1 of size 5. Every element of the array s1 is of type Student.
Programe:
#include<stdio.h>
#include<conio.h>
struct Student
{
int
rno;
char sname[20];
};
void main()
{
struct Student s1[5];
int i;
clrscr();
for(i=0;i<5;i++)
{
printf(“Enter Roll number of Student “);
scanf(“%d”, &s1[i].rno);
printf(“Enter Name of Student “);
scanf(“%s”, &s1[i].sname);
}
for(i=0;i<5;i++)
{
printf(“Roll Number = %d”,s1[i].rno);
printf(“\n Name = %s”, s1[i].sname);
}
getch();
}
Structure within structure
In C, we can declare and place a structure
inside another structure. This is also known as nesting of structure; it is
also known as Structure within Structure. The declaration is same as the
declaration of data type in structure. Structure within structure (or) nesting
of structure is used to create complex records.
Example 1:
struct Student
{
int
rno;
char sname[20];
struct CollegeDetails
{
int collegeNo;
char collName[20];
}
clg;
};
The
above nesting structure can also be written as
struct CollegeDetails
{
int
collegeNo;
char collName[20];
};
struct Student
{
int
rno;
char sname[20];
struct CollegeDetails clg;
};
The programe can be written as
#include<stdio.h>
#include<conio.h>
{
int
collegeNo;
char collName[20];
};
{
int
rno;
char sname[20];
struct CollegeDetails clg;
};
void main()
{
struct Student s1;
clrscr();
printf(“Enter
Roll number of Student “);
scanf(“%d”, &s1.rno);
printf(“Enter Name of Student “);
scanf(“%s”, &s1.sname);
printf(“Enter College Number “);
scanf(“%d”, &s1.clg.collegeNo);
printf(“Enter College Name “);
scanf(“%s”, &s1.clg.collName);
printf(“\n Name = %s”, s1.sname);
printf(“\n College Number = %d”,s1.clg.collegeNo);
printf(“\n College Name = %s”, s1.clg.collName);
getch();
}
Introduction to Union.
Ø A union is a user defined datatype in C programming language.
Ø It is a collection of variables of different datatypes in the same memory location.
Ø We can define a union with many members, but at a given point of time only one member can contain a value.
Ø Union in C language allow data members which are mutually exclusive to share the same memory.
Ø The size of the union is the size of its largest member because sufficient number of bytes must be reserved to store the largest sized member.
Declaration & initialization
Syntax
for declaring a union is same as that of declaring a structure except the
keyword struct.
union name_of_union
{
data_type variableName1 /
MemberName1
data_type variableName2 /
MemberName2
data_type variableName3 /
MemberName3
.
.
.
data_type variableNamen /
MemberNamen
};
Example:
union Student
{
int sno;
char sname[20];
};
#include<stdio.h>
#include<conio.h>
union
Day
{
int dayno;
char dayname[20];
};
void
main()
{
union Day d1;
clrscr();
printf(“Enter Day number “);
scanf(“%d”, &d1.dayno);
printf(“\n Day Number is %d”, d1.dayno);
// will display day number
printf(“\n Enter Day Name “);
scanf(“%s”, &d1.dayname);
printf(“\n Day Name = %s”,d1.dayname); //
will display day name
printf(“\n Day Number = %d”, s1.dayno); // will display garbage value
getch();
}
No comments:
Post a Comment