Structures and Union in C
Structures and Unions in C
Structure
A structure is a user-defined data type in C that allows combining data items of different types into one unit.
➡️ Structures are commonly used to represent a record.
Defining a Structure
To define a structure:
-
Use the keyword
struct -
Followed by an optional structure tag (name)
-
Then the body containing member definitions
Syntax
Example
-
student→ structure name -
S→ structure variable
Important Points
-
Structure members can be:
-
Primitive data types
-
Arrays
-
Pointers
-
Other structures
-
-
A structure cannot contain an instance of itself
-
But it can contain a pointer to itself
-
We can create:
-
Array of structures
-
Pointer to structure
-
Accessing Structure Members
Members are accessed using the dot (.) operator
Example
Initializing a Structure
Structures can be initialized like other data types by assigning values to members.
Example 1: Single Structure
Example 2: Array of Structures
Copying and Comparing Structures
-
Structures can be assigned to another structure of the same type
-
Structures cannot be compared using relational operators (
==,<, etc.)
Union
A union is a special user-defined data type that:
-
Stores different data types
-
Uses the same memory location
-
Only one member holds value at a time
➡️ Memory allocated = size of largest member
Defining a Union
Syntax
Accessing Union Members
-
Similar to structures
-
Uses dot (
.) operator
Example
Output
Similarities Between Structure and Union
-
Both are user-defined data types
-
Used to store different data types in one unit
-
Members can include:
-
Structures
-
Unions
-
Arrays
-
Bit-fields
-
-
Support operators:
-
= -
sizeof
-
-
Assignment allowed only if:
-
Same member types
-
Same structure/union type
-
-
Can be:
-
Passed to functions by value
-
Returned from functions by value
-
-
Members accessed using
.operator
Differences between structure and union
| Feature | Structure | Union |
|---|
| Memory Allocation | Allocates memory for all members separately. Total size = sum of sizes of all members. | Allocates memory for only the largest member. All members share the same memory. |
| Member Access | All members can store values simultaneously. | Only one member can store value at a time. Writing to one member overwrites others. |
| Size | Size = sum of sizes of all members. | Size = size of the largest member. |
| Usage | Used when all members need to be stored and accessed simultaneously. | Used to store different data types in the same memory location (efficient memory usage). |
| Initialization | Can initialize multiple members at once. | Can initialize only one member at a time. |
| Assignment | Structure variables of the same type can be assigned to each other. | Union variables of the same type can also be assigned to each other. |
| Access Operator | Members accessed using . (dot) operator for variables, -> for pointers. | Members accessed using . (dot) operator for variables, -> for pointers. |
| Common Use Cases | Records with multiple fields like student info, employee info, etc. | Memory-efficient storage for different data types (e.g., variant types, embedded systems). |
We can use a union inside a structure to save memory and use the fields efficiently.
Example:
In the above structure stud, you can use either pan or adhar for student identification.
The fields can be accessed as:
Note: Only one field will be used at a time, which helps save memory.
Sample Programs to try
struct Point
{
int x;
int y;
}p1,p2,p3;
int main()
{
printf("Enter the first point(x1,y1)\n");
scanf("%d,%d",&p1.x,&p1.y);
printf("Enter the second point(x2,y2)\n");
scanf("%d,%d",&p2.x,&p2.y);
p3.x=p1.x+p2.x;
p3.y=p1.y+p2.y;
printf("new point after addition\n");
printf("(%d,%d)\n",p3.x,p3.y);
}
#include <stdlib.h>
int main()
{
int N,i,j;
struct stud
{
int adno;
char name[50];
}s[100],t;
printf("enter no of students..N <100::");
scanf("%d",&N);
printf("Enter Admission no and Name of %d students\n",N);
for(i=0;i<N;i++)
scanf("%d %s",&s[i].adno,s[i].name);
//sorting the list according to name
for(i=0;i<N-1;i++)
for(j=0;j<N-1-i;j++)
if(strcmp(s[j].name,s[j+1].name)>0)
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
//printing the roll list in the order of name
printf("Name....Admission Number\n");
for( i=0;i<N;i++)
printf("%-20s%5d\n",s[i].name,s[i].adno);
}
struct library
{
int bookid;
char name[50];
char author[50];
int nc;
}book[100],t;
int main()
{
int n,i,j;
printf("Enter no of books:");
scanf("%d",&n);
printf("Enter the book details.bookid name author number of copies.\n");
#include <stdio.h>
struct student
{
int rno;
char name[50];
char gender[10];
float cgpa;
}stud[100],t;
int main()
{
int n,i,j;
printf("Enter no of students:");
scanf("%d",&n);
printf("Enter the students details rno name gender cgpa \n");
for(i=0;i<n;i++)
scanf("%d %s %s %f",&stud[i].rno,stud[i].name,stud[i].gender,&stud[i].cgpa);
//sorting the stud details in decending order of cgpa
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(stud[j].cgpa<stud[j+1].cgpa)
{
t=stud[j];
stud[j]=stud[j+1];
stud[j+1]=t;
}
//printing the rank list
printf("rno name gender cgpa\n");
printf("***********************************\n");
for( i=0;i<n;i++)
printf("%-8d %-10s%-10s%5f\n",stud[i].rno,stud[i].name,stud[i].gender,stud[i].cgpa);
//list of students with cgpa less than 7
printf("list of students with cgpa less than 7\n");
for( i=0;i<n;i++)
if(stud[i].cgpa<7)
printf("%-8d %-10s%-10s%5f\n",stud[i].rno,stud[i].name,stud[i].gender,stud[i].cgpa);
}
{
int rno;
char name[50];
int m1;
int m2;
int m3;
int tot;
}stud[100],t;
int main()
{
int n,i,j;
printf("Enter no of students:");
scanf("%d",&n);
printf("Enter the students details rno name m1 m2 m3\n");
for(i=0;i<n;i++)
{
scanf("%d%s%d%d %d",&stud[i].rno,stud[i].name,&stud[i].m1,&stud[i].m2,&stud[i].m3);
stud[i].tot=stud[i].m1+stud[i].m2+stud[i].m3;
}
//sorting the stud details in descending order of total mark
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(stud[j].tot<stud[j+1].tot)
{
t=stud[j];
stud[j]=stud[j+1];
stud[j+1]=t;
}
//printing the rank list
printf("rno name m1 m2 m3 total\n");
printf("***********************************\n");
for( i=0;i<n;i++)
printf("%-4d%-10s%-4d%-4d%-4d%-6d\n",stud[i].rno,stud[i].name,stud[i].m1,stud[i].m2,stud[i].m3,stud[i].tot);
}
#include <stdio.h>
struct Time
{
int h;
int m;
int s;
}t1,t2,t3;
struct Time addtime(struct Time t1,struct Time t2)
{
struct Time t3;
int sa,ma;
t3.s=(t1.s+t2.s)%60;
sa=(t1.s+t2.s)/60;
t3.m=(t1.m+t2.m+sa)%60;
ma=(t1.m+t2.m+sa)/60;
t3.h=(t1.h+t2.h+ma)%60;
return t3;
}
int main()
{
printf("Enter the first time(h:m:s)\n");
scanf("%d:%d:%d",&t1.h,&t1.m,&t1.s);
printf("Enter the second time(h:m:s)\n");
scanf("%d:%d:%d",&t2.h,&t2.m,&t2.s);
t3=addtime(t1,t2);
printf("New time t1+t2\n");
printf("%d:%d:%d\n",t3.h,t3.m,t3.s);
}
Comments
Post a Comment