Unit-8 StructuresandUnions
Unit-8 StructuresandUnions
Paneru
UNIT - 8
Structure and Unions
1. Introduction to Structure
Structure is a convenient tool for handling a group of logically related heterogeneous data types. Structure helps to
organize data especially in large programs, because they provide group of variables of different data type to be
treated as a single unit. It is most convenient way to keep related data under one roof.
A structure is usually used when we need to store dissimilar or heterogeneous data together.
The structure elements are stored in contiguous memory location as array.
Structure elements can be accessed through a structure variable using a dot (.) operator.
Structure elements can be accessed through a pointer to a structure using the arrow (->) operator.
All the elements of one structure variable can be assigned to another structure variable using the assignment
(=) operator.
It is possible to pass a structure variable to a function either by value or by reference.
It is possible to create an array of structure i.e. similar type of structure is placed in a common variable
name. For example: we need to store the detail information of individual student in a class.
Ordinary variable
Theordinaryvariable hold one piece of data type.
For example:
int a = 5;
char c = „a‟;
Array variable
The array variable holds a collection of similar data type incontiguousmemorylocation.
For example:
int a[3] = {1,2,3 };
Structure variable
The structure variable holds a collection of different data type.
For example:
struct student
{
char name[20];
int roll_no;
float percentage;
};
3. Declaration of structure
struct structure_name
{
data_type member-1;
data_type member-2;
data_type member-n;
}structure variable;
OR
struct structure_name
{
data_type member-1;
data_type member-2;
By: P.R. Paneru
data_type member-n;
};
struct structure_name structure_variable;
Example of Structure
WAP to create structure of book having name, pages and price. Provide values and display them.
#include<stdio.h>
#include<conio.h>
struct book
{
char name[20];
int pages;
float price;
};
void main()
{
struct book b1;
b1.pages=500;
b1.price=815.5;
strcpy(b1.name, “C Programming”);
printf(“\nName=%s, pages=%d and price=%f”,b1.name,b1.pages,b1.price);
getch();
clrscr();
}
5. Feature of structure
5.1 Structure Assignment
The value of the structure variable can be assigned to another structure variable of the same type using the assignment
(=) operator.
For example-1:
#include<stdio.h>
#include<conio.h>
struct sample
{
int a;
float b;
}x, y;
void main()
{
x.a = 10;
x.b = 15.8;
y=x;
printf("The value is %d and %f", y.a, y.b);
getch();
}
Output:
The value is 10 and 15.80000
For example-2:
#include <stdio.h>
#include <conio.h>
#include <string.h>
By: P.R. Paneru
struct employee
{
char name[20];
char gender;
int age;
};
void main()
{
struct employee e1 = {"Mohan", 'M', 35};
struct employee e2, e3;
strcpy (e2.name, e1.name);
e2.gender = e1.gender;
e2.age = e1.age;
e3=e2;
printf("%s %c %d \n",e1.name, e1.gender, e1.age);
printf("%s %c %d \n",e2.name, e2.gender, e2.age);
printf("%s %c %d \n",e3.name, e3.gender, e3.age);
getch();
}
Output:
Mohan M 35
Mohan M 35
Mohan M 35
#include <stdio.h>
#include <conio.h>
struct num
{
int a;
int b;
}n;
void fun (int, int);
void main()
{
printf("Enter any two numbers:\n");
scanf ("%d %d", &n.a, &n.b);
fun(n.a, n.b);
getch();
clrscr();
}
void fun(int x, int y)
{
int sum;
sum = x+y;
printf("The sum is: %d", sum);
}
Output:
Enter any two numbers:
4
6
The sum is: 10
For example-2:
#include <stdio.h>
#include <conio.h>
void display (char*, char*, int, float); void main()
{
struct book
{
char name[25] ;
char author[25] ;
int page;
float price;
} ;
char name[25] ;
char author[25] ;
int page;
float price;
} ;
For example-2:
#include <stdio.h>
#include <conio.h>
struct book
{
char name[25] ;
char author[25] ;
By: P.R. Paneru
int callno ;
} ;
void display (struct book);
void main( )
{
struct book b1 = { "C-programming", "xyz", 101 } ;
display ( b1 ) ;
}
void display ( struct book b )
{
printf ( "\n%s %s %d", b.name, b.author, b.callno ) ;
getch();
clrscr();
}
Output:
C-programming XYZ 101
Declaration format-2
struct student
{
char name[10];
char address[20];
int class;
};
struct employee
{
char name[10];
char address[20];
char post[10];
struct student s;
}e;
For example:
#include <stdio.h>
#include <conio.h>
struct employee
{
char name[20];
By: P.R. Paneru
float salary;
};
struct address
{
char city[20];
struct employee e;
};
void main()
{
struct address a = {"Dhangadhi", "Ram", 3500};
printf("\n City = %s", a.city);
printf("\n Name = %s", a.e.name);
printf("\n Salary = %f", a.e.salary);
getch();
}
Output:
City = Dhangadhi
Name = Ram
Salary = 3500
For example-2:
#include <stdio.h>
#include <conio.h>
struct student
{
char name[20];
int cls;
float percent;
}st;
void main()
{
By: P.R. Paneru
6. Unions
Both structures and unions are used to group a number of variables of different data type together, called a derived data
type.The union follows the same syntax as structures but union differs from structure in storage and in initialization.
The structure enables us to reserve a separate place in memory for every individual member of the structure. Whereas
with union, it enables to reserve the definite place (memory space of a data type that has highest precision) in memory
and use it for all members of the unions.
Example:
#include <stdio.h>
#include <conio.h>
union data
{
int a;
float b;
};
void main()
{
union data x;
x.a=5;
printf("\nx.a=%d",x.a);
x.b=10.5;
printf("\nx.b=%f",x.b);
getch();
}
7. Self-Referential Structures
Self-referential structure is the structure with pointer member that refer to the structure containing them. Such data
structures are called dynamic data structure. Self-Referential Structures are one of the most useful features. They
allowus to create data structures that contain references to data of the same type as themselves. Self-referential Structure
is used in data structure such as binary tree, linked list, stack, Queue etc.
Syntax
struct structure_name
{
Datatype membername;
struct structure_name * pointer_name;
};