Structure and Unions
4 minute read
STRUCTURE AND UNIONS
What is structure?
·
Structure is a user defined data type which is a collection of data of different data type.
·
Syntax:
Struct
struct_name
{
Data_type variable_name;
------
------
};
Struct struct_name object1, object2;
·
Example:
Struct
books
{
Char title[20];
Int pages;
Float price;
};
struct books book1,book2;
struct books book1,book2;
·
The keyword struct declare a structure to hold the
details of data field like, title, pages and price.
·
Title, pages and
price are called structure element or member.
·
Books is the
name of the structure and is called structure.
·
Book1 and book2as
variable of type struct books.
·
Now we can access
the structure member using structure object and with dot operator.
·
Example:
Book1.pages=250;
Book2.price=12.50;
Strcpy
(book1.title,” COMPUTER”);
Advantages of structure
· It is a
mechanism for packing data of different types.
· It is a convenient tool for
managing a group of logically related data items
·
Structure help in
organizing more complex data in a simple manner.
·
Each member may or may not belong to a different type of data.
· Structure is a user-defined data type
in C that allows you to combine
different data types to store a specific type of record.
·
Structure
is used to represent a record. Suppose we want to store record of Student which consists of student name,
age and roll number for this data we can define a structure to hold this
information.
Array within a structure
arrays may
be the member within structure, this is know as arrays within
structure.
Example
#include<stdio.h>
void main()
{
struct state
{
char name[10];
int dis;
long int pop;
}state;
clrscr();
printf("\n Enter State
name : "); gets(state.name);
printf("\n Enter No. of
Districts : "); scanf("%d",&state.dis);
printf("\n Enter
Population : "); scanf("%ld",
&state.pop);
printf("\n\n\t State
Name >> %s", state.name);
printf("\n\n\t No. of
districts >> %d", state.dis);
printf("\n\n\t
Population >> %ld", state.pop);
getch();
}
Array of structure.
Example
#include<stdio.h>
void main()
{
struct book
{
char title[20],author[20],pub[20];
int year,pages,price;
}book[3];
int i;
clrscr();
for(i=0;i<=2;i++)
{
flushall();
printf("\n Enter Title
:"); gets(book[i].title);
printf("\n Enter Author
:"); gets(book[i].author);
printf("\n Enter
Publisher :"); gets(book[i].pub);
printf("\n Enter Year
:"); scanf("%d",&book[i].year);
printf("\n Enter Pages
:"); scanf("%d",&book[i].pages);
printf("\n Enter Price
:"); scanf("%d",&book[i].price);
clrscr();
}
for(i=0;i<=2;i++)
{
printf("\n\n\n\n Title
is >> %s",book[i].title);
printf("\n\n Author is
>> %s",book[i].author);
printf("\n\n Publisher
is >> %s",book[i].pub);
printf("\n\n Year is
>> %d",book[i].year);
printf("\n\n Pages is
>> %d",book[i].pages);
printf("\n\n Price is
>> %d",book[i].price);
}
getch();
}
Explain Copy of structure variable.
Copy of structure variable
means copy of one variable value to the other variable. Both variable are if
the same structure.
Example
#include<stdio.h>
#include<conio.h>
struct class1
{
int number;
char name[20];
float marks;
};
void main()
{
int x;
struct class1 student1={111,"rao",72.50};
struct class1 student2={222,"reddy",67.00};
struct class1 student3;
student3=student2;
clrscr();
x=((student3.number==student2.number)&&
(student3.marks==student2.marks))? 1:0;
if(x==1)
{
printf("\nstudent2 and student3 are
same");
printf("\n\n%d %s
%f",student3.number,student3.name,student3.marks);
}
else
printf("\nstudent3 and student2 are different");
getch();
}
Explain comparing of structure variable.
Explain comparing of structure variable.
It means
compare one structure variable value with other structure variable. Both variables
are of the same structure.
Example
struct class
{
int no;
char name [20];
float marks;
}
main ( )
{
int x;
struct class stu1 = { 111, “Rao”, 72.50};
struct class stu2 = {222, “Reddy”,
67.80};
struct class stu3;
stu3 = stu2;
x = ( ( stu3.no= = stu2.no) && (
stu3.marks = = stu2.marks))?1:0;
if ( x==1)
{
printf (“ \n student 2 & student 3
are same \n”);
printf (“%d\t%s\t%f “ stu3.no,
stu3.name, stu3.marks);
}
else
printf ( “\n student 2 and student 3 are
different “);
}
Out Put:
Student 2 and student 3 are same
222 reddy 67.0000
Explain nested structure with example.
Structure within structure is called nested structure.
Example
Explain nested structure with example.
Structure within structure is called nested structure.
Example
#include<stdio.h>
main()
{
struct
pop
{
int men,women,female;
struct state
{
int tot;
}state;
}pop;
clrscr();
printf("\n
Enter No of Men :");
scanf("%d",&pop.men);
printf("\n
Enter No of Female :");
scanf("%d",&pop.female);
pop.state.tot=pop.men+pop.female;
printf("\n
TOTAL POPULATION : %d",pop.state.tot);
getch();
}
What is union?
·
Union are a
concept borrowed from structure and there are follow the same syntax as
structure.
·
The major
different difference between them in term of storage.
·
In structure each
member has its own storage location where’s all the member of a union uses the
same location.
·
A union may
contain many numbers of different types.
·
Syntax
union
union_name
{
Data type variable_name;
……….
……….
};
·
Example:
union item
{
Int m;
float x;
char c;
}code;
·
A variable code of type union item.
·
A union contains
three members each with a different data type.
·
However we can
use only one of them at a time.
·
To access a union
member like,
code.m;
code.x;
code.c;
code.m=600;
code.x=62.25;
·
Union may be use
in all places where a structure is allowed.
·
Union may be
initialized when the variable is declared.
Difference between
structure and union.
Structure
|
Union
|
Struct
keyword is used to define a structure
|
Union keyword
is used to define a union.
|
When a member
is connected with a structure, the compiler allocates the memory
for each variable.
|
In case of
Union when a member is connected with a union, the compiler allocates
the memory by considering the size of the largest variable.
|
The size of
structure is more than or equal to the sum of sizes of
its member of structure.
|
The size of
union is equal to the size of largest member of union
|
Each member
within a structure get unique storage area of location
|
Memory
allocation is shared by individual members of union.
|
In Structure,
the address of each variable will be in ascending order This
indicates that memory for each member will start at different offset values
|
in unions the
address is same for all the variable of a union.
|
In Structure,
altering the value of a variable will not affect other members of
the structure
|
in union
altering the value of any of the variable will alter other member
values.
|
In structure
Several members can be initialize at once
|
In unions
only the first variable can be initialized.
|