Structures
Structures are collection of unlike data types. Like arrays,
structure elements are stored in contiguous memory locations.
There are two steps to define a structure.
1. Declaration of a structure.
2. Accessing of structure elements.
Declaring a structure
Syntax:
struct structure name
{
struct element 1;
struct element 2;
struct element 3;
};
e.g.:
struct book
{
char bookname[25];
float price;
int bookno;
};
Accessing structure elements
We can access structure elements by using a dot operator.
Before the dot there must always be a structure variable and after
a dot there must always be a structure element.
E.g.:
Struct book
{
char name[10];
int pages;
float price;
};
struct book b;
e.g.:
b.name
b.pages
b.price
e.g.:
#include<stdio.h>
#include<conio.h>
main()
{
struct book
{
char name[25];
int pages;
float price;
};
struct book b1,b2;
clrscr();
printf("Enter the first book details:\n");
scanf("%s%f%d",b1.name,&b1.price,&b1.pages);
printf("Enter the second book details:\n");
scanf("%s%f%d",b2.name,&b2.price,&b2.pages);
printf("Book details\n");
printf("Name=%s\nPrice=%f\nPages=%d\
n",b1.name,b1.price,b1.pages);
printf("Name=%s\nPrice=%f\nPages=
%d",b2.name,b2.price,b2.pages);
getch();
}
Like array variables, structure variables are also directly initialized
where they are created.
E.g.:
Struct book
{
char name[2];
float price;
int pages;
};
struct book b1={“Basic”,150.40,230};
struct book b2={“C++”,140.60,250};
Array of structures
e.g.:
#include<stdio.h>
#include<conio.h>
main()
{
struct book
{
char name[25];
int pages;
int price;
};
struct book b[10];
int i,n;
clrscr();
printf("Enter how many books:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name,pages,price:\n");
scanf("%s%d
%d",b[i].name,&b[i].pages,&b[i].price);
}
for(i=0;i<n;i++)
printf("Name=%s\nPages=%d\nPrice=%d\
n",b[i].name,b[i].pages,b[i].price);
getch();
}
Structures within structures ( Nested structure)
eg:
#include<stdio.h>
#include<conio.h>
main()
{
struct address
{
char phone[15];
char city[25];
int pin;
};
struct emp
{
char name[25];
struct address a;
};
struct emp e={"Kavitha","530146","nagpur",10};
clrscr();
printf("Name=%s\nPhone=%s\n",e.name,e.a.phone);
printf("City=%s\nPin=%d",e.a.city,e.a.pin);
getch();
}
Uses of structures
strcutures are used in variety of applications .The most
important are in the database management system.
Unions
Unions are similar to structures .The main difference
between these two is a structure enables us to treat a no of
different variables stored at different places in memory. A union
enables us to treat the same space in memory at a no of different
variables.
Uses of unions
They are useful for applications involving multiple
elements , where values need not be assigned to all the elements
at any one of time
Enumerated data types
This gives us an opportunity to invent our own data type
and define what values the variable of this data type can take.
The format of the enum definition is similar to that of a structure.
E.g.:
Enum mar_status
{
single,
married,
divorced,
widowed
};
enum mar_status person 1, person 2;
Like structures, it has two parts:
1. The first part declares the data type and specifies its possible
values. These values are called “enumerators”.
2. The second part declares variables of this data type.
Uses of enum data types
It is used in areas where the values of the variables are
known already in advance.