Structures and Unions Notes
Structures and Unions Notes
STRUCTURES
INTRODUCTION TO STRUCTURES
• A structure is basically similar to records.
• It stores related information about an entity.
• Structure is basically a user defined data type which can store related information
together (even of different data types)
• The major difference between a structure and an array is that an array contains related
information of same data type.
• A structure is a collection of variables under single name. The variables of structure are
of different data types and each has a name that is used to select it from the structure.
• Examples:
❖ Student record: student id, name, branch, fees
❖ Bank account: account number, name, balance
STRUCTURE DECLARATION
• The structure is declared using the keyword struct followed by name of the structure
• All the variables of the structure are declared within the structure
• The syntax is
struct structure-name
{
data_type var-name;
data_type var-name;
-----
};
• For example, if we want to define a structure for a student, the related information is
name, roll_number, course, fees. This can be declared using the structure as
struct student
{
char name [20];
int roll_number;
char course [20];
float fees;
};
• Now the structure has become a user-defined data type. Each var-name declared within
a structure is called a member of the structure.
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
• The structure declaration does not allocate any memory or consume storage space. It
only gives a template to the C compiler how structure looks like and details of the
members
• Similarly, to any other data types, memory is allocated for the structure when we
declare a structure variable. For example, we define variable student by writing, struct
student stu1; where struct student→data type stu1→ variable
• In the following syntax, the variable is declared at the time of declaration.
struct student
{
char name [20];
int roll_number;
char course [20];
float fees;
} stu1, stu2;
In this declaration, we declared two variables stu1 and stu2 of the structure student
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
Example: Declare a structure to store information about an employee
struct Employee
{
int EmpId;
char name [20];
char dept [10];
float salary;
};
Examples
TYPEDEF DECLARATION
• The typedef keyword enables the user to create a new data type name for an existing
data type
• By using typedef, no new data is created, rather an alternative name is given to a known
data type
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
• The general syntax is typedef existing data type new_data type
for example, typedef int INTEGER;
then INTEGER is the new name of data type int
• A typedef declaration is a synonym for the type
for example,
INITIALIZATION OF STRUCTURES
• A structure can be initialized in the same way as other data types initialized. The process
of initializing a structure is assigning some constants to the members of the structure.
• When user does not explicitly initialize the structure, C automatically does it
❖ For int and float, the values are initialized to zero
❖ For char and string members they are initialized to ‘\0’.
• The initializers are enclosed in braces and are comma separated. However, initializers
must match their corresponding types in the structure definition
• When all the members of structure are not initialized, it is called partial initialization.
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
• In case of partial initialization, first few members of the structure are initialized and
those that are uninitialized are assigned default values.
ACCESSING THE MEMBERS OF A STRUTCURE
• Each member in a structure can be used like a normal variable, but its name is longer
• A structure variable can be accessed by using dot (.) operator
• The general syntax is
struct_variable. member_name;
• The dot operator is used to select a particular member of a structure
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
COPYING AND COMPARING STRUCTURE VARIABLES
• We can assign a structure to another structure of the same type
• If we have two structure variables stud1 and stud2 of type struct student is given as
• C does not permit comparison of one structure variable with another. However,
individual members of one structure can be compared with individual members of
another structure
• When we compare one structure member with another structure’s member, the
comparison will behave like any ordinary variable comparison
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
Example Programs
1. Write a program using structures to read and display the information about a
student.
#include<stdio.h>
struct student
{
int roll_no;
char name[20];
char branch[10];
};
void main()
{
struct student s1;
printf(“enter roll.no, name and branch”);
scanf(“%d%s,%s”,&s1.roll_no,s1.name.s1.branch);
printf(“RollNo=%d\nName=%s\nBranch=%s\n”,s1.roll_no,s1.name,s1.branch);
}
2. Write a program using structures to read and display the information about
an Employee.
struct Employee
{
int Id;
char name[20];
float salary;
};
void main()
{
struct Employee E1;
printf(“Enter Employee Id,name and Salary”);
scanf(“%d%s,%f”,&E1.Id,E1.name.e1.salary);
printf(“EmpID=%d\nName=%s\nSalary=%f\n”,E1.Id,E1.name,E1.salary);
}
ARRAYS OF STRUCTURES
• Consider an example of employees working in an organization. In this case, defining a
separate structure for every employee is not a viable solution. Thus, we can have a
common structure definition for all three employees.
• This can be done by declaring an array of structure employees.
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
• An array of structures in C can be defined as the collection of multiple structures
variables where each variable contains information about different entities.
• The array of structures in C are used to store information about multiple entities of
different data types.
• The array of structures is also known as the collection of structures.
• The general syntax is
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
Example program:
#include<stdio.h>
struct student
{
int roll_no;
char name[20];
};
void main()
{
struct student s1[20];
int i,n;
printf(“Enter number of student:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“enter rollno and name student-%d”,i+1);
scanf(“%d%s,&s1[i].roll_no,s1[i].name);
}
for(i=0;i<n;i++)
{
printf(“Details of student-%d”,i+1);
printf(“RollNo=%d\tName=%s\n”,s1*i+.roll_no,s1[i].name);
}
}
Example
struct student
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
{
char name[20];
int rollno;
};
void display(char [],int);
void main()
{
struct student s1;
printf(“enter name &rollno:”);
scanf(“%s%d”,s1.name,&s1.rollno);
display(s1.name,s1.rollno);
}
void display(char n[20],int rn)
{
printf(“Name:%s\n”,n);
printf(“Rollno:%d”,rn);
}
2. Passing the entire structure
• Any changes to structure members within the function are not reflected in the original
structure
• Called function must return entire structure back to the calling function
Example
void display(struct student s);
struct student
{
char name[20];
int rollno;
}
void main()
{
struct student s1;
printf(“enter name & rollno:”);
scanf(“%s%d”,s1.name,&s1.rollno);
display(s1);
}
void display(struct student s1)
{
printf(“Name:%s\n”,s1.name);
printf(“RollNo:%d”,s1.rollno);
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
}
3. Passing the address of the structure
more efficient
Example
struct student
{
char name[20];
int rollno;
}
void display(struct student *s);
main()
{
struct student s1;
printf(“enter name & rollno:”);
scanf(“%s%d”,s1.name,&s1.rollno);
display(&s1);
}
void display(struct student *r)
{
printf(“Rollno:%d”,r->rollno);
printf(“Name:%s”,r->name);
}
Example Program:
Program to display the coordinates (x,y) of a point p using structure and functions
1. Passing individual member
struct point
{
int x;
int y;
};
void display(int, int);
void main()
{
struct point p={2,3};
display(p1.x,p2.y);
}
void display(int a,int b)
{
printf(“the coordinates of the point are:%d %d”,a,b);
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
}
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
UNIONS
INTRODUCTION TO UNIONS
• Union is a collection of variables of different data types
• The difference between the structure and the array is that, in case of unions – we store
information in one field at any one time
• Unions are used to save memory. They are useful for applications that involve multiple
members
DECLARATION OF UNIONS
• The declaration of union is same as that of structure declaration.
• The syntax for union declaration is given by
union union-name
{
data_type var_name;
data_type var_name;
------
};
• The typedef keyword can also be used to simplify the declaration of union variables.
INITIALIZATION OF UNIONS
• The striking difference between the structure and union is that, in case of union – the
field share the same memory space, so fresh data replaces any existing data.
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment
EXAMPLE 1 EXAMPLE 2
Vision: The Department of Computer Science and Engineering shall create professionally competent
and socially responsible engineers capable of working in global environment