0% found this document useful (0 votes)
6 views13 pages

Understanding Structures and Unions in C

The document discusses the concept of structures in programming, explaining their purpose in grouping different data types under a single name for convenience. It covers defining, declaring, accessing, and initializing structures, as well as comparing structures to arrays and demonstrating their use in functions. Additionally, it introduces nested structures and the use of 'typedef' for creating user-defined data types.

Uploaded by

Tharani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Understanding Structures and Unions in C

The document discusses the concept of structures in programming, explaining their purpose in grouping different data types under a single name for convenience. It covers defining, declaring, accessing, and initializing structures, as well as comparing structures to arrays and demonstrating their use in functions. Additionally, it introduces nested structures and the use of 'typedef' for creating user-defined data types.

Uploaded by

Tharani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT-5

STRUCTURE AND UNION


INTRODUCTION STRUCTURE:
✓ S tru c tu re a re u s e d to s to re d iffe re n t typ e s o f d a ta ite m s .
✓ T h e m a in u s e o f s tru c tu re is to c o n v en ie n tly tre a t s u c h c o llec tio n a s a u n it.
✓ s tru c tu re a re u s e d to g ro u p to g e th e r d iff eren t typ e o f v a ria b le s u n d e r th e
sam e nam e .
✓ S tru c tu re is u s ed to re p re fere n c e a rec o rd

N ee d fo r s tru c tu re d a ta typ e
✓ A rra y c a n s to re d a ta 's o f s a m e d a ta typ e .
✓ S tru c tu re c a n s to re lo g ic a lly re la te d d a ta 's o f d iffe ren t d a ta typ e s u n d er a
com m on na m e .

USES AND APPLICATIONS:


✓ C le a rin g s c re en
✓ A d ju s tin g c u rs o r p o s itio n
✓ D ra w ing a n y g rap h ic s s h a p e o n th e s c ree n
✓ rec eivin g a k e y fro m th e k eyb o a rd
✓ fin d in g o u t th e lis t o f e q u ip m en t a tta c h ed to th e c o m p u ter
✓ c h a n g in g th e s iz e o f th e c u rs o r
✓ h id in g file

DEFINING A STRUCTURE:
✓ A s tru c tu re is a c o lle c tio n o f v a ria b le s u n d er a s in g le n am e a n d p ro v id es a
c o n ve n ien t w a y o f g ro u p in g s e ve ra l p ie c e s o f re la te d in fo rm atio n to g eth er.
✓ S tru c tu re c a n b e d efin ed b y u s in g th e k eyw o rd s tru c t
syntax:
EX A M P LE:
s tru c t d a te
{
in t d a te;
c h a r m o n th [2 0 ];
in t yea r;
}
DECLARING STRUCTURE
✓ A f te r d efin in g a s tru c tu re, s tru c tu re va riab le a re d e c la red .
✓ S tru c tu re v a ria b le s d e c la ra tio n s im ila r to d ec la rin g va ria b le o f a ny d a ta typ e s .
Declaring structure:
■ A f te r d efin in g a s tru c tu re, s tru c tu re va riab le a re d e c la red .
■ S tru c tu re v a ria b le s d e c la ra tio n s im ila r to d ec la rin g va ria b le o f a ny
d a ta ty p e.
It in c lu d e f o llo w ing elem e nt
1 ) th e k e yw o rd s tru c t
2 ) th e s tru c tu re n a m e
3 ) lis t o f va ria b les n a m es s ep a rte d b y c o m m a s
4 ) te rm in a tin g s e m ic o lo n

w a ys o f d ec la rin g s tru c tu re v a ria b le


w a y1 : Im m ed ia tely a fter s tru c tu re tem p la te
s tru c t d a te
{
in t d a te;
c h a r m o n th [2 0 ];
in t yea r;
}
to d a y;

w a y2 : D e c la re va ria b le s u s in g s truc t k e yw o rd
s tru c t d a te
{
in t d a te;
c h a r m o n th [2 0 ];
in t yea r;
};
s tru c t d a te to d ay ;

ACCESSING STRUCTURE MEMBERS


A rra y ele m e n ts a re a c c e s s ed u s in g y= th e s u b s c rip t v a ria b le ,s im ila rly s truc tu re
m e m b ers a re a c c es s e d u s in g d o t[.] o p era to r is c a lled a s "s tru c tu re m em b e r o p e ra to r".

e x: s tru c t d a te
{
in t d a te;
c h a r m o n th [2 0 ];
in t yea r;
};
s tru c t d a te to d a y;
to d a y.d a te ;

Initialization of a structure
T h e s tru c tu re v a ria b le c a n b e a ls o b e in itia lize d , b u t th is c a n b e d o n e in c o m p ile tim e
s tru c t d a te to d ay ={"s u n d a y,m o n d a y");

E X :1
# in c lu d e< s td io .h >
# in c lu d e< c o n io . h >
v o id m a in ()
{
s tru c t d a te
{
in t d a y;
in t ye a r;
c h ar *m o n th ;
};
s tru c t d a te d ;
d .d a y= 2 5 ;
d .m o n th = ” m ay ” ;
d .ye a r= 2 0 0 0 ;
p rin tf("d ay =% d m o n th = % s yea r= % d \ n ",d . d a y,d .m o n th ,d .yea r);
}

E X :2
# in c lu d e< s td io .h >
V o id m a in ()
{
S tru c t d em o
{
in t I;
in t j;
}s :
in t i ;
i= 1 0 ;
s .i=1 0 0 ;
s .j= 1 0 1 ;
p rin tf(“ % d % d % d ” I,s . i,s .j);
}

C o m p a ris o n o f s tru c tu re a n d a rra y:


ARRAY STRUCTURE
A n a rra y is a c o llec tio n o f s im ila r d ata A s tru c tu re is a c o llec tio n o f d is s im ila r d a ta
ite m s . ite m s .
A n a rra y is d e riv ed d a ta typ e It is a u s er d e fin e d d ata typ e
It b e h a ve s lik e a b u ilt in d a ta typ e s It m u s t b e d ec la red a n d d e fin ed
A n a rra y c a n b e in c rea s e d o r d e c rea s e d A s tru c tu re e lem en t c a n b e a d d e d if
n ec es s a ry
ARRAY OF STRUCTURE:
✓ C s tru c tu re is a c o lle c tio n o f d iff eren t d a ta typ e w h ic h a re g ro u p ed to g eth er.
✓ W h e re a s a rra y o f s tru c tu re is n o th in g b u t c o llec tio n o f s tru c tu re s . Th is is
a ls o c a lled a s s tru c tu re a rra y in c .
E x a m p le:
s tru c t b o o k in fo
{
c h a r[2 0 ]b n a m e ;
in t p a g e;
in t p ric e;
}
b o o k [1 0 0 ];

E x a m p le p ro g ra m :
# in c lu d e< s td io .h >
# in c lu d e< c o n io . h >
s tru c t b o o k
{
c h a r n a m e [1 0 ];
in t p ric e;
in t p a g es ;
};
S tru c t b o o k b [3 ];
V o id m a in ()
{
in t i;
c lrs c r( );
fo r(i= 1 ;i< = 3 ;i+ + )
{
p rin tf(“ en te r b o o k n a m e,p ric e a n d p a g es :\ n ” );
s c a nf (“ % s % d % d ” ,& b [i]. n a m e ,& b [i].p ric e ,& b [i].p a g es );
}
fo r(i=1 ;i< = 3 ;i+ + )
p rin tf(“ \ n % s \ t% d ” , b [i].n a m e,b [i].p ric e,b [i].p a g e s );
g etc h ( );
}
Output:
E n te r b o o k n a m e, p ric e a n d p a g es :
E n g lis h 1 6 2 2 0 0
E n te r b o o k n a m e, p ric e a n d p a g es :
M a th s 30 0 4 50
E n te r b o o k n a m e, p ric e a n d p a g es :
CP 32 0 31 0
E n g lis h 1 6 2 2 0 0
M a th s 30 0 4 50
CP 32 0 31 0
FUNCTION AND STRUCTURE
s tru c tu re c a n b e p a s s e d a s a rg u m en ts to fu n c tio n .
Th e re a re tw o w a ys
1 )p a s s in g s tru c tu re m em b e rs to fu n c tio n
2 )p a s s in g en tire s tru c tu re to fu n c tio n
PASSING STRUCTURE MEMBERS TO FUNCTION

W e c a n p a s s a m e m b er o f a s tru c tu re to a fu n c tio n b y p a s s in g its v a lu e to th e


fu n c tio n .
PASSING ENTIRE STRUCTURE TO FUNCTION :

W h e n a s tru c tu re is u s ed as a p a ra m eter to a fu n c tio n th e e n tire s truc tu re is


p a s s e d to th e fu n c tio n u s in g th e c a ll b y va lu e m eth o d , h ere a ls o th ere is n o c h a n g e m a d e
ef fec t o n th e p a ra m ete r.

EX A M P LE
# in c lu d e< s td io .h >
# in c lu d e< c o n io . h >
s tru c t s td
{
in t n o ;
flo a t a vg ;
};
V o id fu n ( s tru c t s td p );
V o id m a in ()
{
S tru c t s td a ;
C lrs c r();
a .n o = 1 5 ;
a .a v g = 9 0 .7 5 ;
fu n ( a) ;
}
V o id fu n ( s tru c t s td p )
{
Prin tf(“ n u m b er is … … … % d \ n ” ,p . n o );
Prin tf(“ a ve ra g e is … … … ..% f” ,p .a vg );
}
Output:
N u m b er is … ..1 5
A v era g e is … … .9 0 . 7 5 0 0 0 0

Nested structure (structures within structures)


✓ S tru c tu re w ith in a s tru c tu re is c a lle d a s n es ted s tru c tu res .
✓ A s tru c tu re c a n b e d ec la re d w ith in a n o th e r s tru c tu re . S o m etim es it is re q u ire d to
k ee p a c o m p o u n d d a ta item s w ith in a n o th e r c o m p o u nd d a ta item .
✓ It is u s e d to in c rea s e th e rea d a b ility o f th e p ro g ra m b y re d u c in g th e c o m p lex ity.
E x a m p le:
S tru c t d a te
{
in t d a y;
in t m o n th ;
in t ye a r;
};
S tru c t s tu d e n t
{
C h a r n a m e [2 0 ];
S tru c tu re d a te d o b ;
};
M a in ()
{
S tru c t s tu d e n t ra ja = {“ R a ja ” ,{1 4 ,8 ,8 5 }};
P rin tf(“ \ n n a m e % s ” ,ra ja .n a m e);
P rin tf(“ \ n D a te o f b irth :% d -% d -% d ” ,ra ja . d o b .d a te ,ra ja .d o b . d a y,ra ja . d o b .ye a r);
}

Output
Name : ra ja
D a te o f b irth : 1 4 -8 -9 6
USER DEFINED DATA TYPE – typedef
✓ ‘ C’ p ro vid e s a c a p a b ility th a t e n a b le s th e p ro g ra m m e r to a s s ig n a n a ltern a te
n a m e to a d a ta typ e . T h is is d o n e w ith a s ta tem en t k n o w n a s typedef.
✓ W e c a n c rea te n ew n a m e fo r a lre a d y c re a te d n ew d a ta typ e
✓ T yp e d e f m a k e s o u r c o d e ea s ie r to rea d a n d e a s ier to p o rt to a n ew m a c h in e
✓ W e c a n n o t c rea te n e w p hy s ic a l d a ta typ e u s in g typedef.
SYNTAX
D e s c rip tio n W h ere typ e -s p ec ifie s th e d a ta typ e
D a ta n a m e -s p e c ifie s th e u s e r d efin ed n a m e
fo r tha t typ e .
EX A M P LE T yp e d e f in t w e ek s ;
# in c lu d e< s td io .h >
# in c lu d e< c o n io . h >
# d efin e D 7
v o id m a in ()
{
typ e d e f int w ee k s ;
w e ek s w k ;
c lrs c r( );
p rin tf(“ e n te r w ee k s :” );
s c a n f(“ % d ” ,& w k );
p rin tf(“ n u m b e r o f d a ys = % d ” ,w k *D 7 ) ;
g etc h () ;
}
UNION:
✓ A u n io n is a c o llec tio n o f va ria b les o f d if feren t ty p e ju s t lik e a s tru c tu re .
✓ U n io n is u s ed to m in im ize m e m o ry u tiliz a tio n .
✓ In s tru c tu re , ea c h m em b e r h a s th e s ep a rate s to ra g e lo c a tio n .b ut in u n io n a ll th e
m em b e rs s h a re th e c o m m o n p la c e o f m e m o ry.
SYNTAX:

EX A M P LE:
U n io n res u lt
{
int m a rk s ;
flo a t a vg ;
c a h r g ra d e ;
}s td ;
EX A M P LE :
# in c lu d e< s td io .h >
u n io n n a m e
{
in t a ;
c ha r b [2 ];
};
m a in ()
{
u n io n n a m e c ;
c . a= 2 5 6 ;
p rin tf( “ c .a v a lu e is % d \n ” ,c .a );
p rin tf( “ c .b [0 ]va lu e is % d \ n ” ,c .b [0 ]) ;
p rin tf( “ c .b [1 ] v a lu e is % d \ n ” ,c .b [1 ]);
}
O u tp u t
c .a va lu e… … . 2 5 6
c .b [0 ]v a lu e … … .0
c .b [1 ]v a lu e… … .1

ADVANTAGES OF UNION
1 ) It is u s e d to m in im iz e m em o ry u tiliza tio n .
2 ) It is u s e d to c o n ve rt d a ta fro m o n e typ e to a n o th e r typ e
3 ) It is u s e d to w rite a re c o rd in to a file a s a c h a ra c te r.

STRUCTURE UNION

E v ery m em b e r h a s its o w n m e m o ry A ll the m e m b er u s e th e s a m e m e m o ry


spac e sp a ce

K eyw o rd s tru c t is u s e d K e yw o rd u n io n is u s ed

A ll m e m b ers c a n b e in itializ ed O nly firs t m em b e r c a n b e in itia liz ed

M o re s to ra g e s p a c e re q u ired C o n s erv a tio n o f m e m o ry is p o s s ib le

STORAGE CLASSES:

A s to ra g e c la s s d ef in e s th e s c o p e a n d life tim e o f va ria b le a n d / o r fu n c tio n w ith in


a c p ro g ra m .
S to ra g e _s p ec ifie r d a ta _typ e v a ria b le_ n am e ;
W h e re
S to ra g e_ s p e c ifie r - s p e c ifies th e s to ra g e ( lik e a a u to ,reg is te r,s ta tic ,ex te rn )

TYPES OF STORAGE CLASS SPECIFIERS IN C:

1 .A U T O
2 . R E G IS T E R
3 . S T A T IC
[Link] T ER N

[Link] – STORAGE CLASS:


A u to is th e d e fa u lt c la s s fo r a ll lo c a l v a ria b le s .
V a ria b le in s id e a fu n c tio n is a u to m a tic b y d e fa u lt, k e yw o rd a u to a re ra rely u s ed .

E x a m p le :
# in c lu d e< s td io .h >
v o id in c r(vo id )
{
A u to in t a = 1 ;
P rin tf( “ % \t” ,a ) ;
A =a+1;
}
v o id m a in ()
{
in c r();
in c r();
in c r();
}

Output
1 1 1

REGISTER STORAGE CLASS:


✓ R e g is ter is u s e d to d e fin e lo c a l va ria b le th a t s h o u ld b e s to re d in a reg is ter
in s te a d o f R A M .
✓ R eg is te r v a ria b le are s im ila r to a u to m a tic va ria b le a n d e xis ts in s id e th a t
p a rtic ula r
fu n c tio n o n ly.
✓ R eg is te r s h o u ld o n ly b e u s e d fo r va ria b le th a t req u ire q u ic k a c c es s s u c h a s
c o u n te r.
E x a m p le:
# in c lu d e< s td io .h >
v o id m a in ()
{
reg is te r in t I;
fo r(i= 1 ;i< = 1 0 0 0 ;i+ + )
{
Prin tf(“ /n v a lu e o f i= % d ” ,i);
}
}

OUTPUT:
V a lu e o f i= 1
V a lu e o f i= 2 ;
… … … … … … …
… … … … … … … .
V a lu e o f i= 1 0 0 0

STATIC STORAGE CLASS:


T h e v a lu e o f s ta tic v a ria b le p ers is ts u n til th e e n d o f th e p ro g ra m . v a ria b le c a n b e
d e c la red s ta tic u s ing k eyw o rd , s ta tic .
E x a m p le:
# in c lu d e< s td io .h >
v o id in c r(vo id )
{
S ta tic in t a = 1 ;
p rin tf(“ % \ t” ,a );
A =a+1;
}
v o id m a in ( )
{
in c r( );
in c r( );
in c r( );
}

O u tp u t
1 2 3

EXTERN STORAGE CLASS:

✓ E x tern a l v a ria b le c a n b e a c c e s s ed b y a n y f u n c tio n .


✓ T h ey a re a ls o k n o w n a s g lo b a l v a ria b le s .
✓ V a ria b le s d e c la re d o u ts id e ev ery fu n c tio n a re ex te rna l va ria b le s .
E x a m p le:
F ile 1 :m a in .c
in t c o u n t= 5 ;
m a in ()
{
W rite _ ex te rn ( );
}
F ile 2 :w rite. c
H ere e xtern k eyw o rd is b e g in u s ed to d ec la re c o u n t in a n o th e r file.
N o w c o m p ile th e s e tw o file s a s fo llo w s

PRE PROCESSOR DIRECTIVES:

T h e p re -p ro c es s o r is c o n tro lle d b y d irec tiv es k n o w n a s p re-p ro c es s o r d irec tiv es .


A p re p ro c es s o r d irec tiv es c o n s is ts o f v a rio u s p re-p ro c e s s in g to k en a n d b eg in w ith a #
s ym b o l.

1 . M a c ro re p la c e m en t d ire c tive s (# d efin e ,# u n d ef)


2 . S o u rc e file in c lu s io n (# lin e)
3 . L in e d irec tiv e(# lin e )
4 . E rro r d ire c tive (# e rro r)
5 . P ra g m a d irec tiv es (# p ra g m a )
6 . C o n d itio na l c o m p ila tio n d irec tiv es (# if,# els e,# en d if )
7 . N u ll d irec tiv es (# n e w -lin e )

D IR E C T IV E D E S C R IPT IO N
# d efin e S u b s titu tes a p rep ro c e s s o r m a c ro
# in c lu d e In s ert a p a rtic u la r h e a d e r fro m a n o th er file
# u n d ef U n d efin es a p rep ro c es s o r m ac ro
# ifd ef R e tu rn tru e if th is m a c ro is d e fin e d
# ifn d ef R e tu rn tru e if th is m a c ro is n o t d ef in e d
# if T es ts if a c o m p lie tim e c o n d itio n is tru e
# e ls e T h e a lte rn a tive fo r # if
# e n d if E n d s p rep ro c e s s o r c o n d itio n a l
# p rag m a Is s u es s p ec ia l c o m m a n d s to th e c o m p ile r,u s in g a
s ta n d a rd ize d m eth o d

Refer theory in page:no 5.35


Program for #ifdef,#else and end if in c:

# in c lu d e< s td io .h >
# d efin e R A J U 1 0 0
In t m a in ( )
{
# ifd ef R A J U
P rin tf(“ R A J U is d ef in e d . S o th is lin e w ill b e a d d ed in th e c file \ n ” );
# e ls e
P rin tf( “ R A J U is n o t d e fin ed \n ” );
# e n d if
R e tu rn 0 ;
}

Output:

R A J U is d efin ed . S o th is lin e w ill b e a d d e d in th e C file

You might also like