Structure and Union in C
Structure and Union in C
Q). STRUCTURES:
1. Definition of Structure:
Structure is a collection of a heterogeneous data types , Structure is a user defined data type
and it is a combination of different data types called as members. All these members are referred by a
single name known as structure name. Structure can be placed before or within the main.
Uses of Structures:
1. To maintain database management.
2. Check the memory size of the computer.
3. Sending the output to print.
2. Declaration of Structure:
A structure is declared using the keyword struct followed by a structure name. all the
variables of the structure are declared within the structure.
Syntax to Create Structure:
struct tag_name
{
data type member 1;
..
data type member n;
};
The structure is terminated by a semicolon(;), tag_name such as a name of the structure like
employee.
Example:
struct employee
{
char name[20];
int empno;
int salary;
};
The structure employee holds the three fields. name, empno, salary. These fields are called as
structure elements (or) members. Each member may belongs to a different type of data.
b). We can also create the structure variable along with the structure definition as follows:
Syntax:
struct tagname
{
data type member 1;
..
data type member n;
}structure variable name;
Example:
struct employee
{
int eno;
char ename[20];
float salary;
}emp1, emp2;
4. Initialization of a structure:
A structure can be initialized in the same way as other datatypes are initialized.
Initialization means to assigning some constant to the members of the structure.
Syntax:
struct struct_name
{
Datatype member1;
Datatype member2;
Example:
struct student
{
int rno;
char name[20];
char course[20];
float fee;
} stud={10, Virat BSC,23000 };
(or)
struct student stud={10, Virat BSC,23000 };
We can access members of structure by using dot (.) operator along with the structure variable
name.
Syntax: structure_variable_name.member;
Page- 3
Ex: e.eno;
e.name;
e.sal;
Aim: To write a C-Program to read and print an employee data using structure.
Program:
#include<stdio.h>
#include<conio.h>
struct Employee
{
int eno;
char ename[20];
float esal;
};
void main()
{
struct Employee e;
Array is a collection of similar data types. In the same way we can also define array of structures.
In this structure array, every element is of structure type. The array of structure is declared as follows
In the above example e[10] is an array of 10 elements containing 10 objects of type structure.
Each element of e[10] has structure type with three members that are eno, ename and esal.
The following program illustrates the above concept.
Aim: To Write a C-Program that creates a structure of 10 students having student number
(sno), student name (sname), marks in three subjects (m1,m2,m3), total marks of the
students (tot) and average (avg) and display the names of failed students.
Program:
#include<stdio.h>
#include<conio.h>
struct Student
{
int sno;
char sname[20];
float sfee;
char DOB[80];
};
void main()
{
struct Student s[10];
int n,i;
printf("\nEnter the Number of Students:");
scanf("%d",&n);
}
for(i=0;i<n;i++)
{
printf("******STUDENT DETAILS ARE******:");
printf(%d, s[i].sno);
printf(%s, s[i].sname);
printf(%f, s[i].sfee);
printf(%d, s[i].DOB);
}
getch();
}
Output:
Pointer is a variable can store the address of another variable .The variable may be of any data
type. In the same way we can also define pointer to structure. Here, starting address of the member
variables can be accessed. Thus, such pointers are called structure pointers.
In the above example *ptr is pointer to structure book. The syntax for using pointer with
member is as given below
To pass a any individual elements of the structure to a function, we must use the direct selection
operator to refer to the individual member for the actual parameters.
Whenever a structure element is to be passed to any other function, it is essential to declare the
structure outside the main() function i.e., global.
The following program illustrates the above concept.
void display(point);
void main()
{
struct point;
display(p1)
getch();
}
void display(struct point p1)
Page- 7
{
Printf(the result is%d%d, p.x, p.y);
}
Output:
Syntax:
struct tag_name1
{data_type member1;
Data_type member2;
};
struct tag_name2
{ data_type member1;
Example:
A programe to read and display iformation of a student, using a structure within a structure.
#include<stdio.h>
#include<conio.h>
struct name
{
char fname[20];
char mname[20];
char lname[20];
};
struct student
{
char name[20];
int rno;
float fee;
struct name n;
} struct student s1={ Raj, Sandeep, Sharma, 5, 50000};
void main()
{
struct student s1;
Page- 8
Q). Unions:
A union is a user defined data type available in C that enables us to store different data types in
the same memory location.
We can define a union with many members, but only one member can contain a value at any
given time. Unions provide an efficient way of using the same memory location for multi-purpose.
Example:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
}union Data d1;
int main( )
{
printf( "Memory size occupied by data : %d\n", sizeof(d1));
return 0;
Page- 9
}
When the above code is compiled and executed, it produces the following result:
Memory size occupied by data : 20
int main( )
{
d1.i = 10;
d2.f = 220.5;
printf( " %d", d1.i);
printf( " %f", d1.f);
return 0;
}
OUTPUT:
Here, we can see that values of i and f members of union got corrupted because final value
assigned to the variable has occupied the memory location
void main
{
int i;
p[0].x=2;
p[0].y=3;
p[1].x=4;
p[1].y=5;
for(i=0; i<2; i++)
{
printf(%d%d, p[i].x, p[i].y);
Page- 10
}
getch();
}
Here, type_name is the name of enumerated data type or tag and value1, value2,....,valueN are
values of type type_name.
By default, value1 will be equal to 0, value2 will be 1 and so on but, the programmer can change
the default value as below:
enum suit
{
club=0;
diamonds=10;
hearts=20;
spades=3;
};
enum Boolean
{
false;
true;
};
enum boolean check;
Here, a variable check is declared which is of type enum boolean.
{
enum week today;
today=wednesday;
printf("%d day",today+1);
return 0;
}
Output
4 day
We can write any program in C language without the help of enumerations but, enumerations
helps in writing clear codes and simplify programming.
Important Questions:
1. Define structure and explain the process of accessing structure variables with an example.
2. Explain array of structures with an example.
3. Explain the pointers to structures with an example.
4. Explain how to pass structures to functions with an example.
5. Explain about the Unions in C with an example.
6. Explain about the Enumerated data types in C.