UNIT 9
Structure and union
Structure:
Structure is a collection of variables (can be of different types) under a single name.
For example: You want to store information about a person: his/her name, citizenship number and salary.
You can create different variables name, citNo and salary to store these information separately.
What if you need to store information of more than one person? Now, you need to create different
variables for each information per person: name1, citNo1, salary1, name2, citNo2, salary2 etc.
A better approach would be to have a collection of all related information under a single
name Person structure, and use it for every person.
How to define a structure?
Keyword struct is used for creating a structure.
Syntax of structure
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
Here is an example:
struct Person
{
char name[50];
int citNo;
float salary;
};
Create structure variable
When a structure is defined, it creates a user-defined type. However, no storage or memory is allocated.
To allocate memory of a given structure type and work with it, we need to create variables.
Here's how we create structure variables:
struct Person
{
char name[50];
int citNo;
float salary;
};
int main()
{
struct Person person1, person2, p[20];
return 0;
}
Another way of creating a structure variable is:
struct Person
{
char name[50];
int citNo;
float salary;
} person1, person2, p[20];
How to Access members of a structure?
There are two types of operators used for accessing members of a structure.
1. Member operator(.)
2. Structure pointer operator(->) (will be discussed in structure and pointers)
Suppose, you want to access salary of person2. Here's how you can do it:
[Link]
Example programs
[Link] a program to store and print the roll no., name , age and marks of a student
using structures.
#include <stdio.h>
int main()
{
struct student
{
int roll_no;
char name[30];
int age;
int marks;
};
struct student p1 = {1,"Brown",14,78};
printf("%d %s %d %d\n",p1.roll_no,[Link],[Link],[Link]);
return 0;
}
[Link] a program to accept roll no and marks of 3 subjects of a student,
Calculate total of 3 subjects and average in C language
#include<stdio.h>
#include<conio.h>
main()
int roll_no,m1,m2,m3,total;
float average;
clrscr();
printf("Enter roll number : ");
scanf("%d",&roll_no);
printf("Enter marks 1 : ");
scanf("%d",&m1);
printf("Enter marks 2 : ");
scanf("%d",&m2);
printf("Enter marks 3 : ");
scanf("%d",&m3);
total=m1+m2+m3;
average=total/3.0;
printf("\nStudent Roll Number : %d",roll_no);
printf("\nMarks 1 : %d",m1);
printf("\nMarks 2 : %d",m2);
printf("\nMarks 3 : %d",m3);
printf("\nTotal : %d ",total);
printf("\nAverage : %f ",average);
getch();
}
[Link] a Program to enter the information of 10 students using structure
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s[10];
int main()
{
int i;
printf("Enter information of students:\n");
// storing information
for(i=0; i<10; ++i)
{
s[i].roll = i+1;
printf("\nFor roll number%d,\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<10; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
return 0;
}
Output
Enter information of students:
For roll number1,
Enter name: Tom
Enter marks: 98
For roll number2,
Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
.
.
.
Union
A union is a special data type available in C that allows to store different data types in the same memory
location. You 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 multiple-purpose.
Difference between structure and union
Difference between Structure and Union in C
Structures in C
A structure is a user-defined data type available in C that allows to combining data items of different
kinds. Structures are used to represent a record.
Defining a structure: To define a structure, you must use the struct statement. The struct statement
defines a new data type, with more than one member. The format of the struct statement is as follows:
struct [structure name]
{
member definition;
member definition;
...
member definition;
};
union
A union is a special data type available in C that allows storing different data types in the same memory
location. You 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 multiple purposes.
Defining a Union: To define a union, you must use the union statement in the same way as you did while
defining a structure. The union statement defines a new data type with more than one member for your
program. The format of the union statement is as follows:
union [union name]
{
member definition;
member definition;
...
member definition;
};