Unit-II Structure and Union(25%)
Objective: Understand the user defined data type called structure and unions and its
members and variables
Structures: Introduction, Structure definition, declaration and initialization of
structure, accessing structure member, Comparison of structures and Arrays, Arrays
of structures variable, array within structure ,concept of Structures within Structures,
Structures and functions
Union:
Introduction, union definition, declaration and initialization of union, accessing union
member, Comparison of structure and union
Definition:
Structure is a user defined data type that can store multiple data elements with
different data types.
Data elements of structure should be related.
for example:
book structure contains all data elements about book like book_id,
book_name, book_authorname, book_price, book_totalpages etc.
student structure contains all data elements about student like
student_id, student_name, student_mobileno, student_city etc.
Declaration of Structure:
struct keyword is used to declare a structure.
All data elements/members are declared in { }.
Syntax:
struct structure_name <------user defined data type
{
data element1;
data element2;
-----
data elemetnn;
};
for ex:
struct books <--user defined data type
{
int id;
char name[50]; structure members
char author_name[50];
int price;
};
struct books s1; <-- struct books is user defined data type, s1 is a variable
s1-->id ---->2 bytes
s1-->name[50] ---> 50 bytes
s1-->author_name[50] ---> 50 bytes
s1-->price ---> 2 bytes
Id Name Author_name Price
size of structure books=104 bytes
Declaration of Structure variable:
We can declare structure variable in two ways:
1. At the time of declaration of structure:
struct structure_name <------user defined data type
{
data element1;
data element2;
-----
data elemetnn;
}structure variable;
ex:
struct student
{
int id;
char name[50]; structure members
char address[50];
int contactno;
}s1,s2; <----s1 and s2 are structure variables of student.
2. In main() frunction:
void main()
{
structure variable;
}
ex:
void main()
{
struct student s1,s2;
}
Accessing Structure Members:
We can access structure member using (.) dot operator.
Syntax: structre_variable.structure_member;
ex: [Link]=10
Initialization/Assignment of Structure variables:
we can assign/initialize structure variable using = (assignment operator).
Syntax:
structure_varialbe.structure_member=value;
struct book b1; <--struct book is used defined data type,
b1 is a structure variable.
[Link]=10; <--id is one of the member of book structure.
Program:
//Structure 1 : WAP to create a structure book. Assign values to its members and
display them.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct book
{
int id;
char name[50];
int price;
char author_name[50];
};
void main()
{
struct book b1;
clrscr();
//Assign value
[Link]=56;
strcpy([Link],"C Programming");
[Link]=260;
strcpy(b1.author_name,"Balaguru");
//Display
printf("\nBook id is:%d",[Link]);
printf("\nBook name is:%s",[Link]);
printf("\nBook proce is:%d",[Link]);
printf("\nBook Author name is:%s",b1.author_name);
getch();
}
//Structure 2: WAP to create a structure book. Scan values to its members and
display them.
#include<stdio.h>
#include<conio.h>
struct book
{
int id;
char name[50];
int price;
char author_name[50];
};
void main()
{
struct book b1;
clrscr();
//Scan values from user
printf("\nEnter Book id is:");
scanf("%d",&[Link]);
printf("\nEnter Book name:");
scanf("%s",[Link]);
printf("\nEnter Book price:");
scanf("%d",&[Link]);
printf("\nEnter Book Author name:");
scanf("%s",b1.author_name);
//Display
printf("\nBook id is:%d",[Link]);
printf("\nBook name is:%s",[Link]);
printf("\nBook proce is:%d",[Link]);
printf("\nBook Author name is:%s",b1.author_name);
getch();
}
Comparison of Array and Structure
Array Structure
1. Array is a collection of data elements 1. Structure is a collection of data
with similar data type. elements with different data types.
2. Array is a derived data type. 2. Structure is a user defined data
type.
3. There is not any keyword that is used 3. struct keyword is used to create a
to create an array. structure.
4. Array element is accessed by index 4. Structure element is accessed by dot
value. operator.
5. Array has static memory allocation. 5. Structure has dynamic memory
allocation.
6. Accessing array elements take less 6. Accessing structure elements take
time than structure. more time than array.
Int no[5];
7. Every element in an array has same 7. Every element in a structure can
byte size. have different byte size.
8. Ex. : int a[10]; 8. Ex: struct student
{
int id;
char name[20];
int age;
};
Arrays of structures variable:
Structure is used to store the information of one variable (object). If we need to
store information of multiple variables (objects) then we can create an array of
structure variable.
Syntax: datatype struct_variable_name[size];
ex: struct student s1[5];
In this example, struct student is user defined data type. s1 is array variable.
Program:
//Array of Structure Variable Student: Create a structure of student that stores
information of 5 students.
#include<stdio.h>
#include<conio.h>
struct student
{
int id;
char name[50];
int age;
char address[50];
};
void main()
{
int i;
struct student s1[5];
clrscr();
printf("\nEnter Student Details:\n");
for(i=0;i<=4;i++)
{
//Scan values from user
printf("\nEnter student id :");
scanf("%d",&s1[i].id);
printf("\nEnter Student name:%s");
scanf("%s",s1[i].name);
printf("\nEnter Student Age:");
scanf("%d",&s1[i].age);
printf("\nEnter Student Address:");
scanf("%s",s1[i].address);
}
//Display
printf("\nDisplay of Student Details:\n");
for(i=0;i<=4;i++)
{
printf("\n****************************");
printf("\nStudent id :%d",s1[i].id);
printf("\nStudent name:%s",s1[i].name);
printf("\nStudent Age:%d",s1[i].age);
printf("\nStudent Address:%s",s1[i].address);
}
getch();
}
Structure within Structure:
Nested structure in C is nothing but structure within structure.
One structure can be declared inside other structure as we declare structure
members inside a structure.
//Implementation of Nested Structure
#include<stdio.h>
#include<conio.h>
struct college
{
int college_id;
char college_name[50];
};
struct student
{
int id;
char name[20];
float percentage;
// structure within structure
struct college c1;
};
void main()
{
struct student s1={1, "Priyanka", 90.5, 71145,"Kadi University"};
clrscr();
printf(" Id is: %d \n", [Link]);
printf(" Name is: %s \n", [Link]);
printf(" Percentage is: %f \n\n", [Link]);
printf(" College Id is: %d \n",s1.c1.college_id);
printf(" College Name is: %s \n",s1.c1.college_name);
getch();
}
Structures and functions
We can pass structure variable as an argument in function.
//WAP to create a structure and pass a variable of structure in function. (WANR)
#include <stdio.h>
#include<conio.h>
struct student
{
char name[30];
int age;
};
// function prototype
void display(struct student s);
void main()
{
struct student s1;
clrscr();
printf("Enter name: ");
scanf("%s", [Link]);
printf("Enter age: ");
scanf("%d", &[Link]);
display(s1); // passing struct as an argument
getch();
}
void display(struct student s)
{
printf("\nDisplaying information\n");
printf("Name: %s", [Link]);
printf("\nAge: %d", [Link]);
}
Output
Enter name: Priyanka
Enter age: 13
Displaying information
Name: Priyanka
Age: 13
We can return a structure variable in function.
//WAP to create a structure and return a variable of structure in function. (NAWR)
#include<stdio.h>
#include<conio.h>
struct student
{
char name[50];
int age;
};
// function Declaraction
struct student display();
void main()
{
struct student s;
s =display(); //function calling
printf("\nDisplaying information\n");
printf("Name: %s", [Link]);
printf("\nRoll: %d", [Link]);
}
struct student display() //function definition
{
struct student s1;
printf("Enter name: ");
scanf ("%s", [Link]);
printf("Enter age: ");
scanf("%d", &[Link]);
return s1;
}
Union:
Union Definition:
Union is a user defined data type that has a collection of data elements with
different data types.
Data element of union is accessed by dot(.) operator.
All members share same memory area/location.
Declaration of Union:
union keyword is used to create/declare a union.
Syntax:
union union_name <------user defined data type
{
data element1;
data element2;
------
data elementn;
};
ex:
In above example, union student is a user defined data type.
rollno, gender and marks are union members of student.
Byte size of union= Byte size of largest member.
In above example, rollno requires 2 bytes, gender requires 1 byte, marks
requires 4 bytes. Maximum Byte size is 4, so byte size of union student is 4.
Initialization of union:
In Union, we can initialize only one member at a time.
for ex:
In above student union, we can assign like following:
void main()
{
//union student s1={10,'M',45.5}; It is invalid
union student s1;
clrscr();
[Link]=10;
printf("\nStudent Rollno is:%d",[Link]);
[Link]='M';
printf("\nStudent gender is:%c",[Link]);
[Link]=45.5;
printf("\nStudent Marks is:%f",[Link]);
}
Comparison of structure and union
Structure Union
Struct keyword is used to create union keyword is used to create a union.
structure.
Each member within a structure is Memory allocated is shared by individual
assigned unique storage area of location. members of union.
The size of structure is equal to the sum The size of union is equal to the size of
of size of its members. largest member.
Individual member can be accessed at a Only one member can be accessed at a
time. time.
Several members of a structure can Only the first members of a union can be
initialize at once. initialized.
Union Programs:
//Program: 1 WAP to display byte size of structure and union.
#include<stdio.h>
#include<conio.h>
struct student
{
int id;
char name[50];
int age;
}s1;
union faculty
{
int id;
char name[50];
int age;
}f1;
void main()
{
clrscr();
printf("\n\nByte size of Structure variable is:%d",sizeof(s1));
printf("\n\nByte size of union variable is:%d",sizeof(f1));
getch();
}
//Program 2 :WAP to create a union faculty. Initialize values and display them.
#include<stdio.h>
#include<conio.h>
union faculty
{
int id;
char name[50];
char address[50];
}f1;
void main()
{
clrscr();
printf("\n\nByte size of union variable is:%d",sizeof(f1));
//Initialize values from user
[Link]=10;
printf("\nFaculty id:%d",[Link]);
strcpy([Link],"Priyanka Shah");
printf("\nFaculty name:%s",[Link]);
strcpy([Link],"Gandhinagar");
printf("\nFaculty Address:%s",[Link]);
getch();
}
//Program:
//WAP to create a union student. Read values from user and display them.
#include<stdio.h>
#include<conio.h>
union student
{
int id;
char name[50];
char address[50];
}s1;
void main()
{
clrscr();
printf("\n\nByte size of union variable is:%d",sizeof(s1));
//Scanning values from user
printf("\nEnter Student id:");
scanf("%d",&[Link]);
printf("\nStudent id:%d",[Link]);
printf("\nEnter Student name:");
scanf("%s",[Link]);
printf("\nStudent name:%s",[Link]);
printf("\nEnter Student Address:");
scanf("%s",[Link]);
printf("\nStudent Address:%s",[Link]);
getch();
}