Structures and Unions
Structures and Unions
STRUCTURES
Definition :
Defining a structure
struct structure_name
datatype element1;
datatype element2;
datatype elementN;
};
struct structure_name
datatype element1;
datatype element2;
datatype elementN;
} var1, var2;
Example
struct student
char grades;
int rno;
float avg;
};
struct student
char grades;
int rno;
float avg;
}s1;
Initialization of a structure
struct student
int rno;
int tot;
float avg;
};
struct student
int rno;
int tot;
float avg;
}s1={101, 486,97.20};
#include <stdio.h>
int main() {
struct student
char grades;
int rno;
float avg;
}s1;
printf("%d",sizeof(s1));
return 0;
The amount of the memory space allocated to it is equal to the sum of the memory space required
by all of its members.
struct student
char grades;
int rno;
float avg;
}s1;
Size of the structure =12 bytes
(4+4+4)
struct student
char name[20];
int tot;
float avg;
}s1;
(20+4+4)
struct student
int rno;
float avg;
}s1,s2;
s2.rno, s2.avg;
⇨ Structure_variable . member_name
#include<stdio.h>
int main()
struct student
char name[20];
int rno;
};
scanf("%s%d",s1.name,&s1.rno);
Nested Structures
struct namelist
char fname[20];
char lname[10];
};
struct phonebook
char mobile[15];
};
Array of Structures
• 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.
struct student
{
int rno;
float avg;
}s[50];
• In such cases the structure elements are accesses with for loop as s[i].rno,s[i].avg
• ie for(i=0;i<50;i++)
• scanf(“%d %f ” ,&s[i].rno,&s[i],avg);
#include<stdio.h>
struct student
char name[20];
int rno;
};
int main()
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s%d",s[i].name,&s[i].rno);
for(i=0;i<n;i++)
printf("\n %s %d",s[i].name,s[i].rno);
}}
#include<stdio.h>
struct student
int rno;
int tot;
float avg;
};
main()
s2=s1;
printf("%d%d%f",s1.rno,s1.tot,s1.avg);
printf("%d%d%f",s2.rno,s2.tot,s2.avg);
• We can pass a structure as a function argument just like we pass any other variable or an array as a
function argument.
#include<stdio.h>
struct Student
char name[10];
int roll;
};
Pointers to structures
• It is possible to create a pointer to a structure type. It can store the address of a structure variable.
struct stud
……….
……….
#include<stdio.h>
struct student
int rno;
float tot;
};
int main()
{
s2=&s1;
printf("\n %d %f",s1.rno,s1.tot);
printf("\n %d %f",s2->rno,s2->tot);
return 0;
Output:
101 95.000000
101 95.000000
• Self Referential Structure is the Data Structure in which the pointer refers (points) to the structure
of the same type. It used in the many of the data structures like in Linked List, Trees, Graphs, Heap
etc.
Example:
struct node
int data;
}newnode;
Here, the members are accessed using -> (arrow) operator as follows
newnode->data;
newnode->link;
Union
• Like structure, Union in c language is a user-defined data type that is used to store the different
type of elements.
• All the aspects and the operations of union are same that of structures. The only difference
between them is in terms of storage of their memories.
• In structure, a separate memory is allotted to each member, while in unions, all the members of an
object share the same memory.
• The size of a union is equal to the size of the largest data member.
Initialization of Unions
union object can hold the value of only one of its member at a time. Hence while initializing a union
object, it is allowed to initialize its first member only.
union student
{ int rno;
int m1,m2,m3;
float avg;
};
#include<stdio.h>
int main()
union student
char name[20];
int rno;
};
printf("Enter Name\n");
scanf("%s",s1.name);
scanf("%d",&s1.rno);
printf("\nroll no is %d",s1.rno);
#include<stdio.h>
union student
char name[20];
int rno,m1,m2,m3,tot;
float avg;
};
int main()
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",s[i].name);
printf("Name %s\t",s[i].name);
scanf("%d",&s[i].rno);
printf("Rollno %d\t",s[i].rno);
scanf("%d",&s[i].m1);
printf("Mark 1 %d\t",s[i].m1);
temp=s[i].m1;
scanf("%d",&s[i].m2);
printf("Mark 2 %d\t",s[i].m2);
temp=temp+s[i].m2;
scanf("%d",&s[i].m3);
printf("Mark 3 %d\t",s[i].m3);
temp=temp+s[i].m3;
s[i].tot=temp;
printf("Total %d\n",s[i].tot);
s[i].avg=temp/3;
printf("Average %f\n",s[i].avg);
Structure union
different data type under a single name. data type under a single name.
Declared with the keyword struct followed Declared with the keyword union followed by
by structure name and set of elements union name and set of elements
Each member has its own storage space All the members share the same memory
It can handle all the members at the same It can handle only one element at a time
time
Example
} s; } s;
size of the above structure is 9 size of the union is 4 bytes [ maximum sized
bytes[1+4+4] element]
{ int rno;
float avg;
};
union stud
{ int rno;
float avg;
};
Ans: A
Ans: B
Ans: B
Ans: A
Ans: A
Ans: B
#include<stdio.h>
int main()
struct num
int i, j, k, l;
};
(Compilation error)
#include<stdio.h>
struct
int i;
float ft;
}decl;
int main()
decl.i = 4;
decl.ft = 7.96623;
return 0;
Output: 4 7.97
#include<stdio.h>
int main()
struct leader
char *lead;
int born;
};
#include<stdio.h>
int main()
struct st
int i;
};
return 0;
(Compilation error)