Structures Union and
Enumerated data type
Subject Code: CSE101R02
Subject Name: Problem Solving & Programming in C
Dr. A. Joy Christy
AP III/SoC
SASTRA Deemed to be University, Thanjavur
Structure
• A structure is a user-defined data type that can store related information under one name
• Difference between structure and array
• Array stores multiple values of same datatype in one name
• Structure is a collection of variables of different datatype under a single name
• Structure is similar to records
• Stores related information about an entity
Structure declaration
Initializing structure variable
• struct data_type_name structure_variable_name;
• Ex:
• struct student stud1;
• Accessing data members of the structure
• struct_varabile_name.member_name=value;
• stud1.r_no=101;
• [Link]=50000;
Copying and Comparing Structures
Student program
Nested structures
• Structure within another structure
Array of structures
• Creation of array of structure variables
for(i=0;i<n;i++)
#include<stdio.h>
{
struct student
printf("Enter name, rno, dept\n");
{
scanf("%s%d%s",s[i].name,&s[i].rno,s[i].dept);
char name[20];
printf("Enter three marks\n");
int rno;
scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
char dept[20];
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
int m1,m2,m3,tot;
s[i].avg=(float)s[i].tot/3.0;
float avg;
}
};
printf("Students details are:\n");
void main()
for(i=0;i<n;i++)
{
{
struct student s[20];
printf("\nName: %s",s[i].name);
int i,n;
printf("\nRoll No: %d",s[i].rno);
printf("Enter the number of students\n");
printf("\n Dept:%s",s[i].dept);
scanf("%d",&n);
printf("\nTotal:%d",s[i].tot);
printf("\nAverage:%f",s[i].avg);
}
}
Passing structures to functions
#include<stdio.h>
struct student void print_details(struct student x)
{ {
char name[20]; printf("Student Detail\n");
int rno; printf("\nName: %s",[Link]);
char dept[20]; printf("\nRoll No:%d",[Link]);
}; printf("\nDept: %s",x.d)
void print_details(struct student); }
void main()
{
struct student s;
printf("Enter name\n");
scanf("%s",[Link]);
printf("Enter roll no\n");
scanf("%d",&[Link]);
printf("Enter Dept\n");
scanf("%s",[Link]);
print_details(s);
}
#include<stdio.h>
struct complex
{ int real;
float im; void sub_complex(struct complex a,struct complex b)
}; {
void add_complex(struct complex,struct complex); struct complex c;
void sub_complex(struct complex,struct complex); [Link]=[Link];
void mul_complex(struct complex,struct complex); [Link]=[Link];
int main() printf("\nReal:%d",[Link]);
{ struct complex c1,c2,c3; printf("\nImaginary: %f",[Link]);
printf("Enter values for first complex number\n"); }
scanf("%d%f",&[Link],&[Link]); void mul_complex(struct complex a,struct complex b)
printf("\nEnter values for second complex number\n"); {
scanf("%d%f",&[Link],&[Link]); struct complex c;
add_complex(c1,c2); [Link]=[Link]*[Link]*[Link];
sub_complex(c1,c2); [Link]=[Link]*[Link]+[Link]*[Link];
mul_complex(c1,c2); printf("\nReal:%d",[Link]);
} printf("\nImaginary: %f",[Link]);
void add_complex(struct complex a,struct complex b) }
{ struct complex c;
[Link]=[Link]+[Link];
[Link]=[Link]+[Link];
printf("\nReal:%d",[Link]);
printf("\nImaginary: %f",[Link]);}
Passing array of structures to functions
void get_details(struct student s[],int n)
{
#include<stdio.h> int i;
struct student for(i=0;i<n;i++)
{ {
char name[20]; printf("Enter name, rno, dept\n");
int rno; scanf("%s%d%s",s[i].name,&s[i].rno,s[i].dept);
char dept[20]; printf("Enter three marks\n");
int m1,m2,m3,tot; scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
float avg; s[i].tot=s[i].m1+s[i].m2+s[i].m3;
}; s[i].avg=(float)s[i].tot/3.0;
void get_details(struct student[],int); }
void disp_details(struct student[],int); }
void main() void disp_details(struct student s[],int n)
{ {
struct student s[20]; int i;
int i,n; printf("Students details are:\n");
printf("Enter the number of students\n"); for(i=0;i<n;i++)
scanf("%d",&n); {
get_details(s,n); printf("\nName: %s",s[i].name);
disp_details(s,n); printf("\nRoll No: %d",s[i].rno);
} printf("\n Dept:%s",s[i].dept);
printf("\nTotal:%d",s[i].tot);
printf("\nAverage:%f",s[i].avg);
}
}
Passing structures through pointers
#include<stdio.h>
struct student void print_details(struct student *x)
{ {
char name[20]; printf("Student Detail\n");
int rno; printf("\nName: %s",x->name);
char dept[20]; printf("\nRoll No:%d",x->rno);
}; printf("\nDept: %s",x->dept);
void print_details(struct student *); }
void main()
{
struct student s;
printf("Enter name\n");
scanf("%s",[Link]);
printf("Enter roll no\n");
scanf("%d",&[Link]);
printf("Enter Dept\n");
scanf("%s",[Link]);
print_details(&s);
}
Self-referential Structures
• Structures that contain a reference to data of its same type
Unions
• Union is a collection of variables of different data types
• Only difference – store information in one field at any one time
Unions
• Union is a collection of variables of different data types
• Only difference – store information in one field at any one time
Enumerated Data type
• User-defined data type based on standard integer type
• Consists of named integer constants
enum variables
Enumeration type conversion
Comparing Enumerated types
Input/output operations on enumerated types