Unit 10- Structure and union
Unit 10- Structure and union
Structure:
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.
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;
};
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;
}
struct Person
{
char name[50];
int citNo;
float salary;
} person1, person2, p[20];
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:
person2.salary
Example programs
1.Write 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,p1.name,p1.age,p1.marks);
return 0;
}
#include<stdio.h>
#include<conio.h>
main()
int roll_no,m1,m2,m3,total;
float average;
clrscr();
scanf("%d",&roll_no);
scanf("%d",&m1);
scanf("%d",&m3);
total=m1+m2+m3;
average=total/3.0;
printf("\nMarks 1 : %d",m1);
printf("\nMarks 2 : %d",m2);
printf("\nMarks 3 : %d",m3);
printf("\nTotal : %d ",total);
printf("\nAverage : %f ",average);
getch();
}
3.Write 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;
// storing information
for(i=0; i<10; ++i)
{
s[i].roll = i+1;
printf("\nFor roll number%d,\n",s[i].roll);
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
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.
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: