STRUCTURE IN C
What is Structure
• Structure in c is a user-defined data type that
enables us to store the collection of different
data types. Each element of a structure is
called a member.
• The struct keyword is used to define the
structure.
syntax to define the structure in c
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeberN;
};
Example:
struct employee
{ int id;
char name[20];
float salary;
};
The following image shows the memory allocation of
the structure employee that is defined in the above
example.
Declaring structure variable
We can declare a variable for the structure so
that we can access the member of the
structure easily. There are two ways to declare
structure variable:
• By struct keyword within main() function
• By declaring a variable at the time of defining
the structure.
1st way:
• Let's see the example to declare the structure
variable by struct keyword. It should be
declared within the main function.
struct employee
{ int id;
char name[50];
float salary;
};
Now write given code inside the main() function.
struct employee e1, e2;
2nd way:
• Let's see another way to declare variable at
the time of defining the structure.
struct employee
{ int id;
char name[50];
float salary;
}e1,e2;
NOTE:
Which approach is good
• If number of variables are not fixed, use the
1st approach. It provides you the flexibility to
declare the structure variable many times.
• If no. of variables are fixed, use 2nd approach.
It saves your code to declare a variable in
main() function.
Accessing members of the structure
There are two ways to access structure
members:
• By . (member or dot operator)
• By -> (structure pointer operator)
Let's see a simple example of structure in C language.
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
}e1; //declaring e1 variable for structure
int main( )
{
//store first employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
//printing first employee information
printf( "employee 1 id : %d\n", e1.id);
printf( "employee 1 name : %s\n", e1.name);
return 0;
}
Output:
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
C Array of Structures
• An array of structres 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.
Let's see an example of an array of structures that stores information of 5 students and prints it.
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++){
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++){
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
Output:
Enter Records of 5 students
Enter Rollno:1
Enter Name:Sonoo
Enter Rollno:2
Enter Name:Ratan
Enter Rollno:3
Enter Name:Vimal
Enter Rollno:4
Enter Name:James
Enter Rollno:5
Enter Name:Sarfraz
Student Information List:
Rollno:1, Name:Sonoo
Rollno:2, Name:Ratan
Rollno:3, Name:Vimal
Rollno:4, Name:James
Rollno:5, Name:Sarfraz
C Nested Structure example
#include <stdio.h>
#include <string.h>
struct Employee
{
int id;
char name[20];
struct Date
{
int dd;
int mm;
int yyyy;
}doj;
}e1;
int main( )
{
//storing employee information
e1.id=101;
strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array
e1.doj.dd=10;
e1.doj.mm=11;
e1.doj.yyyy=2014;
//printing first employee information
printf( "employee id : %d\n", e1.id);
printf( "employee name : %s\n", e1.name);
printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
return 0;
}
Output:
employee id : 101 employee name : Sonoo Jaiswal
employee date of joining (dd/mm/yyyy) : 10/11/2014
Passing structure to function
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
void display(struct employee);
void main ()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
display(emp);
}
void display(struct employee emp)
{
printf("Printing the details....\n");
printf("%s %s %d %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
Unions
• Union can be defined as a user-defined data
type which is a collection of different variables
of different data types in the same memory
location.
• The union can also be defined as many
members, but only one member can contain a
value at a particular point in time.
• Union is a user-defined data type, but unlike
structures, they share the same memory
location.
Defining a Union
union [union tag]
{
member definition;
member definition;
...
member definition;
} [one or more union variables];
Example:
union Data
{
int i;
float f;
char str[20];
} data;
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
OUTPUT:
Memory size occupied by data : 20
EXAMPLE:
union abc{
int a;
char b;
float c;
double d;
};
int main()
{
printf("Size of union abc is %d", sizeof(union abc));
return 0;
}
• As we know, the size of int is 4 bytes, size of
char is 1 byte, size of float is 4 bytes, and the
size of double is 8 bytes.
• Since the double variable occupies the largest
memory among all the four variables, so total
8 bytes will be allocated in the memory.
• Therefore, the output of the above program
would be 8 bytes.
Struct Union
The struct keyword is used to define a The union keyword is used to define union.
structure.
When the variables are declared in a structure, When the variable is declared in the union,
the compiler allocates memory to each the compiler allocates memory to the largest
variables member. The size of a structure is size variable member. The size of a union is
equal or greater to the sum of the sizes of equal to the size of its largest data member
each data member. size.
Each variable member occupied a unique Variables members share the memory space
memory space. of the largest size variable.
Changing the value of a member will not affect Changing the value of one member will also
other variables members. affect other variables members.
Write a program to use structure within union , display the contents of
structure elements
#include<stdio.h>
#include<conio.h>
void main() {
struct student {
char name[30];
char sex;
int rollno;
float percentage;
};
union details {
struct student st;
};
union details set;
printf("Enter details:");
printf("\nEnter name : ");
scanf("%s", set.st.name);
printf("\nEnter roll no : ");
scanf("%d", &set.st.rollno);
printf("\nEnter sex : ");
scanf("%c", &set.st.sex);
printf("\nEnter percentage :");
scanf("%f", &set.st.percentage);
printf("\nThe student details are : \n");
printf("\name : %s", set.st.name);
printf("\nRollno : %d", set.st.rollno);
printf("\nSex : %c", set.st.sex);
printf("\nPercentage : %f", set.st.percentage);
getch();
Enter details:
Enter name : Pritesh
Enter rollno: 10
Enter sex: M
Enter percentage: 89
The student details are:
Name : Pritesh
Rollno : 10
Sex : M
Percentage : 89.000000