CSE101-Lec#27
• Nested Structure
• Union
©LPU CSE101 C Programming
Outline
• Nested Structure
• Union
©LPU CSE101 C Programming
Nested Structure
• Nested structures are structures as member of
another structure.
• We can also take objects of one structure as
member in another structure.
• Thus, a structure within a structure can be used
to create complex data application.
• Dot operator is used twice because we are
accessing first structure through the object of
second structure.
©LPU CSE101 C Programming
Nested Structure
Two ways of declaring structure within
structure or Nested structure:
• Declare two separate structures
• Embedded structures
©LPU CSE101 C Programming
Example-1-Declare two separate
structures(standalone structures)
struct Date
{
int dd;
int mm;
int yy;
};
struct Student
{
char name[20];
int rollno;
int marks;
struct Date dob;
};
Here structure Student is nesting structure and structure date is nested
structure
©LPU CSE101 C Programming
Program example-1 Nested structure-Declare two separate
structures(standalone structures)
#include<stdio.h>
struct Address
{ scanf("%f",&E.Salary);
char Housename[25]; scanf("%s",E.Add.Housename);
char City[25];
scanf("%s",E.Add.City);
char Streetname[25];
}; scanf("%s",E.Add.Streetname);
struct Employee printf("\n\tEmployee Id : %d",E.Id);
{ printf("\n\tEmployee Salary :
int Id; %f",E.Salary);
char Name[25]; printf("\n\tEmployee House No :
float Salary; %s",E.Add.Housename);
struct Address Add; printf("\n\tEmployee City :
}; %s",E.Add.City);
int main()
printf("\n\tEmployee street name:
{
%s",E.Add.Streetname);
struct Employee E;
scanf("%d",&E.Id); }
scanf("%s",E.Name);
©LPU CSE101 C Programming
• // You are using GCC
• #include<stdio.h>
• struct Address
• {
• char Housename[25];
• char City[25];
• char Streetname[25];
• };
• struct Employee
• {
• int Id;
• char Name[25];
• float Salary;
• struct Address Add;
• };
• int main()
• {
• struct Employee E;
• scanf("%d %s %f %s %s %s",&E.Id,E.Name,&E.Salary,E.Add.Housename,E.Add.City,E.Add.Streetname);
• printf("Employee Id : %d\n Employee Name: %s\n Employee Salary : %f\n ",E.Id,E.Name,E.Salary);
• printf("\n\tEmployee House No : %s",E.Add.Housename);
• printf("\n\tEmployee City : %s",E.Add.City);
• printf("\n\tEmployee street name: %s",E.Add.Streetname);
• }
•
©LPU CSE101 C Programming
Example-2: embedded structures(Nested structure)
struct Student
{
char name[20];
int rollno;
struct date
{
int dd;
int mm;
int yy;
} dob;
};
©LPU CSE101 C Programming
Program example-2 Nested structure-Embedded structure
#include <stdio.h>
struct Employee
{
char ename[20];
int ssn;
float salary;
struct dob
{
int date;
int month;
int year;
}db1;
}emp = {"Aniket",1000,1000.50,{22,6,1990}};
int main()
{
printf("\nEmployee Name : %s",emp.ename);
printf("\nEmployee SSN : %d",emp.ssn);
printf("\nEmployee Salary : %.2f",emp.salary);
printf("\nEmployee DOB :
%d/%d/%d",emp.db1.date,emp.db1.month,emp.db1.year);
return 0;
}
©LPU CSE101 C Programming
Unions
• union
– Memory that contains a variety of objects over time
– Only contains one data member at a time
– Members of a union share space
– Conserves storage
– Only the last data member defined can be accessed
• union definitions
– Same as struct
union Number {
int x;
float y;
};
union Number value;
©LPU CSE101 C Programming
Union
• Union is similar as structure. The major
distinction between them is in terms of storage.
• In structure each member has its own storage
location whereas all the members of union uses
the same location.
• The union may contain many members of
different data type but it can handle only one
member at a time union can be declared using
the keyword union.
©LPU CSE101 C Programming
Example
• A class is a very good example of structure and union in
this example students are sitting in contiguous memory
allocation as they are treated as a structure individually.
And if we are taking the place of teacher then in a class
only one teacher can teach. After leaving the first teacher
then another teacher can enter.
©LPU CSE101 C Programming
Union Declaration
union item
{
int m;
float x;
char c;
}code;
This declare a variable code of type union item
©LPU CSE101 C Programming
Initializing and accessing union members
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}
©LPU CSE101 C Programming
Program using union
#include <stdio.h>
union job{
char name[32];
float salary;
int worker_no;
}u;
int main()
{
printf("Enter name:\n");
scanf("%s",&u.name);
printf("Enter salary: \n");
scanf("%f",&u.salary);
printf("Displaying\nName :%s\n",u.name);
printf("Salary: %.1f",u.salary);
}
©LPU CSE101 C Programming
WAP to read and display one record using union
#include<stdio.h>
union employee
{
char name[30];
int id;
float salary;
}u;
int main()
{
//union employee u;
printf("\n Enter name:");
gets(u.name);//Initialization
printf("\n Entered name is:%s",u.name);//Accessing
printf("\n Enter id:");
scanf("%d",&u.id);//Initialization
printf("\n Entered id is:%d",u.id);//Accessing
printf("\n Enter salary:");
scanf("%f",&u.salary);//Initialization
printf("\n Entered salary is:%.2f",u.salary);//Accessing
return 0;
}
©LPU CSE101 C Programming
WAP to read and display n number of records using Array of
Unions
#include<stdio.h> printf("\n Entered name is:
union employee %s",u[i].name);
{
char name[30];
printf("\n Enter id:");
int id; fflush(stdin);
float salary; scanf("%d",&u[i].id);
}u[100];
int main()
printf("\n Entered id is:
{ %d",u[i].id);
//union employee u[100]; printf("\n Enter salary:");
int n,i; fflush(stdin);
printf("\n Enter value of n:");
scanf("%d",&n);
scanf("%f",&u[i].salary);
fflush(stdin); printf("\n Entered salary is:
for(i=0;i<n;i++) %.2f",u[i].salary);
{ }
printf("\n Enter name:");
fflush(stdin);
return 0;
gets(u[i].name); }
©LPU CSE101 C Programming
Difference between structure and union
©LPU CSE101 C Programming
Similarities between structure and union
• Both are user-defined data types used to store data of different
types as a single unit.
• Their members can be objects of any type, including other
structures and unions or arrays. A member can also consist of a bit
field.
• Both structures and unions support only assignment = and sizeof
operators. The two structures or unions in the assignment must
have the same members and member types.
• A structure or a union can be passed by value to functions and
returned by value by functions. The argument must have the same
type as the function parameter. A structure or union is passed by
value just like a scalar variable as a corresponding parameter.
• ‘.’ operator is used for accessing members.
©LPU CSE101 C Programming
Macro
• A macro is a piece of code in a program that
is replaced by the value of the macro.
• Macro is defined by #define directive.
• Whenever a macro name is encountered by
the compiler, it replaces the name with the
definition of the macro.
• Macro definitions need not be terminated by a
semi-colon(;).
©LPU CSE101 C Programming
Object Macro
// C program to illustrate macros
#include <stdio.h>
// Macro definition
#define LIMIT 5
// Driver Code
int main()
{
// Print the value of macro defined
printf("The value of LIMIT is %d",LIMIT);
return 0;
}
©LPU CSE101 C Programming
Function Macro
// C program to illustrate macros
#include <stdio.h>
// Function-like Macro definition
#define min(a, b) (((a) < (b)) ? (a) : (b))
// Driver Code
int main()
{
// Given two number a and b
int a = 18;
int b = 76;
printf("Minimum value between %d and %d is %d\n",a, b, min(a, b));
return 0;
}
©LPU CSE101 C Programming