User Defined Datatypes: Structure & Union
Need:
- An array can be used to represent a collection of data items of similar types. If we want to make a collection of different data types, array cannot do this job. To
solve this problem, C provides two constructed datatypes – Structure & Union.
Both are user defined datatypes.
Structure:
=> A STRUCTURE is
a tool used for handling a group of logically
related data items.
=> Using a
STRUCTURE, we can make a compound datatype using
different primary data types.
=> There are many data members in STRUCTURE but represented as single entity (unit).
=> The data members may be similar or
different type.
=> If a STUDENT is a structure then its members may be RollNo,
Name, Marks, etc.
=> Every structure member has its own
storage allocation in memory.
(Every structure member is allocated in separate memory space.)
=> All the members
can be accessed at the same
time in the program.
=> It is used to
represent complex data structures.
=> It is used to pass arguments to the function, which may lead to
minimize no. of arguments.
=> It is also used
to return more than one values from called
function.
=> It is mostly
used to manage records in database management
applications.
=> It can be
defined either global or local in the program.
~~~~~~~~~~~~@~~~~~~~~~~~~
Defining Structure:
=> A keyword “struct” is used to define a STRUCTURE.
=> It can be
defined inside or outside (before) main( )
function.
Syntax:
struct structure-name
{
datatype member-1;
datatype member-2;
datatype member-3;
};
Ex:
struct STUD
{
int RN;
char NAME[10];
float PER;
};
~~~~~~~~~~~~@~~~~~~~~~~~~
Declaring a Structure:
Syntax:
struct structure-name variable;
Ex:
struct
STUD X;
struct
STUD S1, S2, S3;
Initialization of a Structure:
Syntax:
struct structure-name variable={value-1, value-2, value-3, . . . .
};
Ex:
struct
STUD X={1001, “RAM”, 79.28};
~~~~~~~~~~~~@~~~~~~~~~~~~
Accessing Data-members of a Structure:
=> A
member access operator is used to access data-members
of a structure:
Dot Operator => .
(for Simple Structure)
Arrow Operator => -> (for Pointer to Structure)
Syntax:
structure-variable . member-name;
Ex:
X .RN;
printf(“Roll Number = %d”, X.RN);
~~~~~~~~~~~~@~~~~~~~~~~~~
// An
example program for STRUCTURE. STRUCT-1.C
#
include<stdio.h>
#
include<conio.h>
struct STUD //Global Structure
{
int RN;
char NAME[10];
float PER;
};
void
main( )
{
struct STUD X={1001, “RAM”, 79.28}; //Initialization
clrscr( );
printf(“\n Roll Number = %d”, X.RN);
printf(“\n Name = %s”, X.NAME);
printf(“\n Percentage = %f”, X.PER);
printf(“\n Size of X= %d”, sizeof(X));
}
~~~~~~~~~~~~@~~~~~~~~~~~~
Array of Structure:
=> We can create an
Array of Structure like an array of simple variable.
=> This is treated as a simple array.
=> This can be
used to manage more than one record of Student or Product information.
Ex:
struct STUD
S[3];
// An
example program for Array of Structure. STRUCT-3.C
//
Managing records of 3 students.
#
include<stdio.h>
#
include<conio.h>
struct STUD
{
int RN;
char NAME[10];
float PER;
};
void
main( )
{
int i;
struct
STUD S[3]={ {1001, “RAM”, 79.28} , {1002,
“SHYAM”, 66.22} ,
{1003, “AMIT”, 72.99} };
clrscr( );
printf(“Roll No. Name
Percentage”);
for(i=0; i<=2; i++)
{
printf(“\n%d %s
%f ”, S[ i ].RN, S[ i ].NAME,
S[ i ].PER);
}
printf(“\n Good Day”);
}
~~~~~~~~~~~~@~~~~~~~~~~~~
Pointer to
Structure:
=> A pointer can
point a STRUCTURE like a simple variable.
=> We will use
Arrow ( -> ) operator to access a structure
member.
(in place of dot
operator)
Ex:
struct
STUD X, *P;
P=&X;
Accessing Data-members of a Structure:
printf(“Roll Number = %d”, P->RN); (in place of X.RN)
Structure
|
|
Accessing its members
|
|
In simple way
|
With Pointer
|
Using (.)
dot operator
|
Using (->) arrow operator
|
X.NAME
|
P->NAME
|
// An
example program for Pointer to Structure. STRUCT-4.C
#
include<stdio.h>
#
include<conio.h>
struct STUD
{
int RN;
char NAME[10];
float PER;
};
void
main( )
{
struct
STUD X={1001, “RAM”, 79.28};
struct
STUD *P;
clrscr( );
printf(“Student Information using Structure…”);
printf(“\n Roll Number = %d”, X.RN);
printf(“\n Name = %s”, X.NAME);
printf(“\n Percentage = %f”, X.PER);
P=&X;
printf(“\n\n Student Information using Pointer to Structure…”);
printf(“\n Roll Number = %d”, P->RN);
printf(“\n Name = %s”, P->NAME);
printf(“\n Percentage = %f”, P->PER);
}
~~~~~~~~~~~~@~~~~~~~~~~~~
Passing Structure to the Function:
=>
We can simply pass a structure to the function like a
simple variable.
void demo(struct STUD
Z)
{
-----------
-----------
-----------
}
void
main( )
{
struct STUD
X;
-----------
-----------
demo(X);
-----------
-----------
}
// An
example program for passing a STRUCTURE to the FUNCTION. STRUCT-1.C
#
include<stdio.h>
#
include<conio.h>
struct STUD
{
int RN;
char NAME[10];
float PER;
};
void demo(struct STUD Z)
{
printf(“\n Student
Information from UDF…”);
printf(“\n My Roll
Number = %d”, Z.RN);
printf(“\n My Name =
%s”, Z.NAME);
printf(“\n My
Percentage = %f”, Z.PER);
}
void
main( )
{
struct
STUD X={1001, “RAM”, 79.28};
clrscr( );
printf(“\n Student Information from
main( ) function…”);
printf(“\n Roll Number = %d”, X.RN);
printf(“\n Name = %s”, X.NAME);
printf(“\n Percentage = %f”, X.PER);
demo(X);
printf(“\n Good Day”);
}
~~~~~~~~~~~~@~~~~~~~~~~~~
Structure within a structure:
=> We know that a
structure can be implemented with functions and arrays. Similarly, a structure
can be implemented as the member of another
structure.
=> When a
structure is declared as the member of another
structure, it is called Structure within a structure. It is also known as Nested Structure.
Example:
struct DATE
{
int
DD;
int MM;
int YY;
};
struct STUD
{
int RN;
char NAME[10];
float PER;
struct
DATE DOB;
};
----------------------------------OR----------------------------------
Example:
struct STUD
{
int RN;
char NAME[10];
float PER;
struct DATE
{
int DD;
int MM;
int YY;
} DOB ;
};
//
Input following information of a student and prints it using “Structure within Structure”: STRUCT-2.C
Roll Number, Name, Marks and DOB.
#
include<stdio.h>
#
include<conio.h>
struct DATE
{
int
DD;
int MM;
int YY;
};
struct STUD
{
int RN;
char NAME[10];
float PER;
struct DATE DOB; // Structure within a
structure
};
void
main( )
{
struct
STUD X; //Declaration
clrscr( );
printf(“Input Roll Number = “);
scanf(“%d”, &X.RN);
printf(“Input Name = “);
scanf(“%s”, &X.NAME);
printf(“Input Percentage = “);
scanf(“%f”, &X.PER);
printf(“Input Day for DOB = “);
scanf(“%d”, &X.DOB.DD);
printf(“Input Month for DOB = “);
scanf(“%d”, &X.DOB.MM);
printf(“Input Year for DOB = “);
scanf(“%d”, &X.DOB.YY);
printf(“\n Student Information…”);
printf(“\n Roll Number = %d”, X.RN);
printf(“\n Name = %s”, X.NAME);
printf(“\n Percentage = %f”, X.PER);
printf(“\n Date of Birth = %d-%d-%d”, X.DOB.DD,
X.DOB.MM, X.DOB.YY);
}
~~~~~~~~~~~~@~~~~~~~~~~~~
UNION:
=> A UNION is also
a user defined datatype like structure.
=> Both are used
to represent a group of logically related data
items.
=> There is a major
difference between them is Memory storage. In structure each member has its own memory storage allocation, where as union uses
only one memory storage allocation to manage all
the members.
=> Union shares
single (common) memory area for all the members.
=> We can access only one member at a time in Union.
=> Its applications
are very limited in Computer as compare to Structures.
=> All the Union
members can not be initialized
all together.
(We never initialized union members unlike structure.)
=> It can be
defined either global or local in the program.
Structure
|
Union
|
struct STUD
{
int RN; => 2 Bytes
char
NAME[10]; => 10 Bytes
float
PER; => 4 Bytes
};
|
union STUD
{
int RN; => 2 Bytes
char NAME[10]; =>
10 Bytes
float
PER; => 4 Bytes
};
|
Size
of Structure = Total No. of bytes of all the members
|
Size
of Union = No. of bytes of the LARGEST member
|
Size
of Structure = 16 Bytes
|
Size
of Union = 10 Bytes
|
Defining Union:
=> A keyword “union” is used to define a UNION.
=> It can be
defined inside or outside
(before) main( ) function.
Syntax:
union union-name
{
datatype member-1;
datatype member-2;
datatype member-3;
};
Ex:
union STUD
{
int RN;
char NAME[10];
float PER;
};
Declaring a Union:
Syntax:
union union-name
variable;
Ex:
union
STUD X;
union
STUD S1, S2, S3;
Accessing Data-members of a Union:
=> A
Dot Operator (.)
is used to access data member of a Union:
Syntax:
union-variable . member-name;
Ex:
X.RN;
printf(“Roll Number = %d”, X.RN);
~~~~~~~~~~~~@~~~~~~~~~~~~
// An
example program for UNION. UNION-1.C
#
include<stdio.h>
#
include<conio.h>
#
include<string.h>
union STUD
{
int RN;
char NAME[10];
float PER;
};
void
main( )
{
union STUD X;
clrscr( );
X.RN=1001;
strcpy(X.NAME, ”RAM”);
X.PER=79.28;
printf(“\n Roll Number = %d”, X.RN); =>
?
printf(“\n Name = %s”, X.NAME); =>
?
printf(“\n Percentage = %f”, X.PER); =>
79.28
printf(“\n Size of Union X in Bytes =
%d”, sizeof(X)); => 10
}
~~~~~~~~~~~~@~~~~~~~~~~~~
Difference between Structure and Union:
=>
Both are used to handle logical related data items like Student which may
contain information Roll Number, Name, Class, Marks, etc.
=>
Both are contained members of different types and represented as single entity.
The only difference is in memory management.
=>
Both are treated as user defined data type.
=>
Both are very different from an Array because it can contain only similar types
of data but structure & union can contain any types of data. (may be
similar or not)
=>
Both are defined in the same way. (Syntax of definition and declaration are
same.)
~~~~~~~~~~~~@~~~~~~~~~~~~
Difference between Structure and Union:
Structure
|
Union
|
1. Every structure member is allocated in separate memory space.
(Uses separate memory for every member.)
|
1. In union, all members are stored in a single memory space.
(Uses common
memory for all the members.)
|
2.
All members of structure can be initialized.
|
2.
Only one member of a union can be initialized.
|
3. We
can access all the members of structure at anytime.
|
3. Only
one member of union can be accessed at a time.
|
4. It
is commonly used in most of the applications.
|
4. It
is not commonly used, have a special used.
|
5.
'struct' keyword is used to declare structure.
|
5.
'union' keyword is used to declare union.
|
6. Syntax:
struct struct_name
{
member
1;
member
2;
----------
----------
};
|
6. Syntax:
union
struct_name
{
member
1;
member
2;
----------
----------
};
|
7. Example:
struct STUD
{
int
rno;
char
name[10];
float marks;
};
|
7. Example:
union
STUD
{
int
rno;
char name[10];
float marks;
};
|
8. Structure variable declaration:
struct
STUD X;
|
8. Union variable declaration:
union
STUD Y;
|
Allocated
memory for X => 2+10+4 = 16 bytes.
|
Allocated
memory for Y => 10 = 10 bytes.
Because
“name” is the largest member.
|
Size
of Structure will be equivalent to sum of all the
member size.
|
Size
of Union will be equivalent to the largest member
size.
|
~~~~~~~~~~~~@~~~~~~~~~~~~
Structure
|
Array
|
Structure is a User
Defined Data-type.
|
Array is a Derived
Data-type.
|
Structure is a
collection of DIFFERENT types of data.
|
Array is a
collection of SIMILAR types of data.
|
Structure members
are accessed by a DOT operator.
|
Array elements are
accessed by an INDEX variable.
|
Memory location of
structure members may different.
|
Memory location of
array elements should be in an order/contiguous.
|
There are NO
further type of structure.
|
There are 3 types
of an array:
-
One Dimensional Array
-
Two Dimensional Array
-
Multi Dimensional Array
|
Syntax:
struct
Structure-Name
{
datatype
member-1;
datatype
member-1;
datatype
member-1;
};
|
Syntax:
data-type variable [size];
|
Example:
struct STUD
{
int
RN;
char
NAME[10];
float
PER;
};
|
Example:
int N[10];
|
~~~~~~~~~~~~@~~~~~~~~~~~~
No comments:
Post a Comment