Structure and Union
Structure and Union
1 Introduction
Structure is basically a user-defined data type that can store related information (even of different
data types) together.
V The major difference between a structure and an array is that an array can store only information of
same data type.
A structure is a collection of variables under a single name. The variables within a structure are
of different data types and each has a name that is used to select it from the structure.
*A Structure is a user defined data type, which is used to store the values of different data types
together under the same name".
5.1.1Structure Declaration
A structure is declared using the keyword struct followed by the structure name.
Allthe variables of the structure are declared within the structure.
A structure type is generally declared by using the following syntax:
struct struct-name
struct student studi;
fees
int r_no;
fees
char name[20):
char course[20]:; Figure 5.1 Memory allocation for a structure
float fees; variable
int r no;
char name[20];
char course[20]:
float fees;
}studl, stud2;
studl and stud2 of the structure student.
5.1.3 Initialization of Structures
Initializing a structure means assigning some constants to the members of the structure.
When the user does not explicitly initialize the structure, then C automatically does it.
For int and float members, the values are initialized to zero, and char and string members are
initialized to "\0' by default.
V The initializers are enclosed in braces and are separated by commas.
V The general syntax to initialize a structure variable is as follows:
struct struct name
int r_no;
char name(20];
char course|20|:
float fees:
}studl = {01, "Rahul'", "BCA", 45000;;
Or.
by writing,
struct student studl = {01, "Rahul", "BCA", 45000};
struct student stud1 struct student stud2 {07, "Rajiv"};
- (01, "Rahul", "BCA", 45000):
0 Rahul BCA 45000 07 Rajiv 10 0.0
1. Simple Addition
In this techniques, make a list of all data types and add the memory required by each.
Consider a simple structure of an employee
struct Employee
void main()
struct employee e;
printf("%d", sizeof(e);
}
Example: C program to read and display the student details using structures.
#include<stdio.h>
struct student
int rnum;
char name[20]:
int marks;
}s[60):
void main()
int in;
printf("Enter the number of students"):
5.3 Union
Like structure, a union is a collection of variables of diferent data types. The only difference
between a structure and a union is that in case of unions, you can only store information in one field
at any one time.
To better understand union, think of it as a chunk of memory that is used to store variables of different
types. When a new value is assigned to a field, the existing data is replaced with the new data.
/ Again, the typedef keyword can be used to simplify the declaration of union variables.
The most important thing to remember about a union is that the size of an union is the size of its
largest field. This is because a sufficient number of bytes must be reserved to store the largest sized
field.
Int x, y,
int x;
int y;