You are on page 1of 7

Structures and Unions

9.1. INTRODUCTION
Arrays can be used to represent a group of data items that belong to the
same type, such as int or float. However, if we want to represent a collection of
data items of different types using a simple name, then we cannot use an array.
Fortunately, C supports a constructed data type known as structure, which is a
method for packing data of different types.
A structure is a collection of one or more variables, possibly of different data
types, grouped together under a single name for convenient handling. For
example, it can be used to represent a set of attributes, such as empno
(employee number, name and bpay (basic pay. !he concept of a structure is
analogous to that of a "record# in many other languages.
Consider an example. $n an organi%ation, an employee#s details (i.e., his
number, name and basic pay have to be maintained. &ne potential method would
be to create a two'dimensional array, which could contain all the details. (ut in
this case, this is not possible because the parameters are of different types, i.e.,
empno is of type integer, name of type char and bpay of type float. !hese
different types data of an employee can be clubbed together and referred by a
single name in C by the use of an aggregate data type called structure. $t will also
be possible to address each of these different data items in a structure
individually, in addition to addressing them collectively by a single name.
9.2. STRUCTURE DEFINITION
A structure definition creates a format that may be used to declare structure
variables. !he syntax is )
struct <structure_name>
{
data_type member1 ;
data_type member2 ;
. . .
. . .
. . .
data_type membern ;
} structure_variables ;
9.2 Structures and Unions
*here struct is a keyword, following the keyword struct, structure+name is
called a structure tag and is optional, which is used to identify the structure. $t is
optional because it only names this new type that has been defined by the
structure declaration, and can be used as a substitute later. structure+variables
are declared like ordinary variables. ,ore than one variable may also be declared
by placing a comma in between the variables. A structure declaration that is not
followed by a list of variables reserves no storage spaces, it merely describes the
template. $n other words, the structure is only defines, not declared.
-et us use an example to illustrate the process of structure definition and the
creation of structure variables. Consider a student database consisting of regno
(register no, name and percent (percentage. *e can define a structure to hold
this information as follows )
struct student
.
int regno /
char name0123 /
float percent /
4 /
!he keyword struct declares a structure to hold the details of three fields,
namely regno, name and percent. !hese fields are called structure elements or
members. 5ach member may belong to a different type of data. student is the
name of the structure and is called the structure tag. !he tag name may be used
subse6uently to declare variables that have the tag#s structure.
!he above declaration has not declared any variables. $t simply describes a
format called template to represent information as shown below )
struct student
regno 7 bytes
name 12 bytes
percent 8 bytes
*e can declare the structure variables using the tag name anywhere in the
program. For example, the statement )
$nteger
Array of 12 characters
Float
Structures and Unions 9.3
struct student stud1, stud7, stud9 /
:eclares stud1, stud7 and stud9 as variables of type struct student. 5ach one
of these variables has three members as specified by the template. !he complete
declaration might look like as follows )
struct student
.
int regno /
char name0123 /
float percent /
4 /
struct student stud1, stud7, stud9 /
;emember that the members of a structure themselves are not variables.
!hey do not occupy any memory until they are associated with the structure
variables such as stud1. $t is allowed to combine both the template declaration
and variables declaration in one statement as follows )
struct student
.
int regno /
char name0123 /
float percent /
4 stud1, stud7, stud9 /
!he above declaration is also valid. !he use of tag name is optional. For
example )
struct
.
int regno /
char name0123 /
float percent /
4 stud1, stud7, stud9 /
*ill :eclares stud1, stud7 and stud9 as structure variables representing three
students, but not include a tag name for later use in declarations. <ormally,
structure definitions appear at the beginning of the program file, before any
variable or functions are defined.
Note : !he structure name i.e., tag or the structure variables may be
omitted, but both should not be omitted.
9.4 Structures and Unions
9.3. DOT OPERATOR
!he dot operator (. is used to refer to individual members of a structure. !he
dot operator is also called as structure member operator or a period operator. !he
syntax of referring a structure member is )
structure_name.member_name ;
For example )
stud1.regno /
$s the variable representing the register number (regno of stud1 and can be
treated like any other ordinary variable.
!herefore to assign a value to the structure variable is as follows )
stud1.regno = 7>>11>1 /
!his assigns the number 7>>11>1 to the structure member regno. ?imilarly to
display the regno on the scree, the statement is as follows )
printf(@AdB, stud1.regno /
*e can also use scanf( function to give the values through the keyboard as
follows )
scanf(@Ad As AfB, Cstud1.regno, stud1.name, Cstud.percent /
Consider the following program for finding the total and average of a students
for 2 subDects )
! De"onstration o# structures !
$ inc%ude &stdio.'(
$ inc%ude &conio.'(
struct student
)
int ro%%no *
c'ar na"e+1,- *
int "ar.s+,-/ tota% *
#%oat a01 *
2 st *
Structures and Unions 9.5
0oid "ain34
)
int i *
c%rscr34 *
st.tota% 5 6 *
7rint#38Enter t'e ro%% nu"9er : 84 *
scan#38:d8/ ;st.ro%%no4 *
7rint#38<nEnter t'e na"e : 84 *
scan#38:s8/ st.na"e4 *
7rint#38<nEnter "ar.s in , su9=ects : <n<n84 *
#or3i 5 6 * i & , * i>>4
)
scan#38:d8/ ;st."ar.s+i-4 *
st.tota% 5 st.tota% > st."ar.s+i- *
2
st.a01 5 st.tota% , *
7rint#38<nNo. <t Na"e <t Tota% <t A0era1e <n<n84 *
7rint#38:d <t :s <t :d <t :.2# 8/ st.ro%%no/ st.na"e/ st.tota%/
st.a014 *
1etc'34 *
2
RUN 1 :
Enter t'e ro%% nu"9er : 161
Enter t'e na"e : Arun
Enter "ar.s in , su9=ects :
?6 ?, ?6 ?, ?6
No. Na"e Tota% A0era1e
161 Arun 3@6 ?2.66
9.A. UNIONS
A union is similar to a struct, except it allows you to define variables that
share storage space. $n structures, each member has its own storage location,
whereas all the members of a union use the same location. !his implies that,
although a union may contain many members of different types, it can be handle
only one member at a time. -ike structures, a union can be declared using the
keyword union. !he syntax is )
9.6 Structures and Unions
union <union_name>
{
data_type member1 ;
data_type member2 ;
. . .
. . .
. . .
data_type membern ;
} union_variables ;
*here union is a keyword, following the keyword union, union+name is called
a union tag and is optional, which is used to identify the union. $t is optional
because it only names this new type that has been defined by the union
declaration, and can be used as a substitute later. union+variables are declared
like ordinary variables. ,ore than one variable may also be declared by placing a
comma in between the variables. A union declaration that is not followed by a list
of variables reserves no storage spaces, it merely describes the template. $n other
words, the union is only defines, not declared.
-et us use an example to illustrate the process of union definition and the
creation of union variables. Consider a student database consisting of regno
(register no, name and percent (percentage. *e can define a union to hold this
information as follows )
union student
.
int regno /
char name01>3 /
float percent /
4 /
!he keyword union declares a union to hold the details of three fields, namely
regno, name and percent. !hese fields are called union elements or members.
5ach member may belong to a different type of data. student is the name of the
union and is called the union tag. !he tag name may be used subse6uently to
declare variables that have the tag#s union.
!he above declaration has not declared any variables. $t simply describes a
format called template. !he compiler allocates a piece of storage that is large
enough to hold the largest variable type in the union. $n the declaration above,
the member name re6uires 1> bytes (array of 1> characters i.e. 1> x 1 byte
among the members. Figure below shows how all the three variables share the
same address.
Structures and Unions 9.7
union student
?torage of 1> bytes
82>> 82>1 82>7 82>9 82>8 82>2 82>E 82>F 82>G 82>H
 regno 
integer
 percent 
float
 name 
Array of 1> characters
Note : 5lements of a union are accessed in the same manner as a struct.