Structure and Union
Structure and Union
BSC NOTES
PDF
COLLECTION
www.avashkattel.com.np/bscnotes
Unit 8 Structure and Union (5
Hrs)
Contents of the unit
• Introduction, array of structure, Passing structure to functions,
passing array of structure to function, structure within structure
(nested Structure), union, pointer to structure
C Structure
• We can declare a variable for the structure so that we can access the
member of the structure easily. There are two ways to declare
structure variable:
• By struct keyword within main() function
• By declaring a variable at the time of defining the structure.
Comparison of Array and Structure
• Array is a collection of data items of same type and structure is simply
defined as the collection of data items of different types
• We can list differences as
Array Structure
An array is a collection of related data A structure can have elements of
elements of same type different types
An array is derived data type Structure is a user-defined data type
An array behaves line built in type Structure does not behave like a built in
types
Array name is a pointer to the first Structure name is not pointer
element of it
Structure Initialization
• We can initialize the members of structure like ordinary variables.
• The members to be initialized must appear in order as in the
definition of structure within braces and separated by commas.
• For example:
• struct student {
• int rn;
• char name[20];
•}
• struct student s = {1,”Kamal”};
Accessing the members of Structure
• The members of a structure are usually processed individually as
separate entity.
• Therefore, we must be able to access the individual structure
members.
• A structure members can be accessed by using period or dot(“.”).
• The syntax is:
• structure_variable.member
Example
Array of Structure
• Like ordinary variables we can create an array of structure.
• In this case, the array will have individual structure as its elements.
• Synax:
• struct structure_name array_name[size];
Example
Initializing Array of Structure
• We can initialize an array of structure in the same way as a single structure.
• Let us consider the following example:
• struct book
• {
• char name[20];
• int price;
• int pages;
• };
• Struct book b[2] = {
• “C programming”, 200, 300,
• “ Java programming”, 300,400
• }
•
Nested Structure(Structure within Another
Structure)
• One structure may be nested within another structure’s definition.
• The outer structure is known as nesting structure and the inner is called
nested structure.
• For example
• struct date Struct student
•{ {
char name[20];
• int day; struct date dob;
• int month; int rn;
};
• int year;
•}
Pointer to Structure
• A pointer can also be used with a structure.
• To store address of a structure variable, we can define a structure type pointer variable in normal way.
• Example:
• Struct student
• {
• char name[20];
• struct date dob;
• int rn;
• };
• Then we can define structure variable and pointer variable of the structure type
• Struct student s;
• Struct student *p;
• p= &s;
Function and Structure
• Like an ordinary variable, a structure variable can also be passed to a
function.
• We may either pass individual structure elements or the entire
structure to a function as its argument.
• #include<stdio.h>
• struct Employee
• {
• int eid;
• char name[20];
• int salary;
• };
• void print(struct Employee );
• int main()
• {
• struct Employee e;
• printf("Enter eid, name and salary of employee\n");
• scanf("%d%s%d",&e.eid,e.name,&e.salary);
• printf("Employee Information\n");
• print(e);
• return 0;
• }
• void print(struct Employee emp)
•{
• printf("Empoyee Name:%s\n",emp.name);
• printf("Employee Salary %d\n",emp.salary);
• printf("Employee EID: %d\n",emp.eid);
•}
Union
• A union is similar to a structure with respect to syntax and use.
• The union also contains members whose individual data types may
different from one another.
• The distinction is that all members within a union share the same
storage area of computer memory, whereas each member within a
structure is assigned its own unique storage.
• Since same memory is shared by all its members, only one variable
can reside into a memory at a time.
• Thus, the union is used to conserve memory space.
Union
• Union is not useful for applications involving members where the
values need to be assigned to all of the members at the same time.
• Therefore, although a union may contain many members of different
types, it can handle only one member at a time.
• The compiler allocates a piece of storage that is large enough to hold
the largest variable type in the union
Defining Union
• union union_name
•{
• data_type 1 member1
• data_type 2 member 2
• ….
• data type n member n
• };
Example
• union ContacInfo
•{
• char phno [10];
• char perma_add [20];
• char temp_add [20];
• char emailId [20];
• };
• Example: Write a C program to demonstrate union
• #include<stdio.h>
• union Number
• {
• int a;
• float b;
• double c;
• };
• int main()
• {
• union Number n;
• printf("Enter integer number\n");
• scanf("%d",&n.a);
• printf("Ingere number is %d\n",n.a);
• printf("Enter float number\n");
• scanf("%f",&n.b);
• printf("float number is %f\n",n.b);
• printf("Enter double number\n");
• scanf("%f",&n.c);
• printf("double number is %f\n",n.c);
• }
Difference between Structure and Union
Structure Union
1 Each member of a structure is All members of a union share the same storage
assigned its own unique storage. area of computer memory.
3 The amount of memory required The amount of memory required to store a union is
to store a structure is the sum of the same as memory size occupied by a member which
memory sizes required by its requires largest memory space among the
members members
4 All the members of structure can Only one member of union can be accessed at any
be accessed at any point of time given time
• struct distance
• {
• int meters;
• int centimeters;
• };
•
• d1 = 100m, 50 cm
•
• #include <stdio.h>
• struct distance
• {
• int meter;
• int centimeter;
•
• };
• int main()
• {
• struct distance d1,d2;
• printf("Enter distance in meter\n");
• scanf("%d",&d1.meter);
• printf("Enter distance in centimer meter\n");
• scanf("%d",&d1.centimeter);
• printf("Distance d1 is %dm %dcm\n",d1.meter,d1.centimeter);
• d2.meter = d1.meter*5;
• d2.centimeter = d1.centimeter*5;
• d2.meter = d2.meter+d2.centimeter/100;
• d2.centimeter = d2.centimeter%100;
• printf("Distance d2 is %dm %dcm\n",d2.meter,d2.centimeter);
• }