Date post: | 05-Mar-2016 |
Category: |
Documents |
Upload: | zul-faqih-jaini |
View: | 15 times |
Download: | 0 times |
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 1/43
CHAP 7 : STRUCTURE AND POINTER
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 2/43
Chapter : STRUCTURE AND POINTER
7.1 Pointer and string 7.2 Str!tre and pointer 7." Nested str!tre 7.# Arra$ and str!tre
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 3/43
%C% Progra&&ing 'ith Str!treRe!ords
Purpose of structuresCoding a structure template
Defining a new data typeFunctions which communicate
using structuresUse of structure pointersStructures and arraysAssi nin structures
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 4/43
Lect 23 P. 4
Data Str!tres (str!t)
Arra$s re*ire that a++ e+e&ents ,e o- thesa&e data t$pe. an$ ti&es it is ne!essar$ togrop in-or&ation o- di/erent data t$pes. An
e0a&p+e is a &ateria+s +ist -or a prod!t. The+ist t$pi!a++$ in!+des a na&e -or ea!h ite& apart n&,er di&ensions eight and !ost.
C and C33 spport data str!tres that !an
store !o&,inations o- !hara!ter integer4oating point and en&erated t$pe data. The$ are !a++ed a structs.
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 5/43
Lect 23 P. 5
Str!tres (str!t)
A struct is a deri5ed data t$pe !o&posed o-&e&,ers that are ea!h -nda&enta+ orderi5ed data t$pes.
A sing+e struct o+d store the data -or oneo,6e!t. An arra$ o- struct s o+d store thedata -or se5era+ o,6e!ts.
A struct !an ,e dened in se5era+ a$s asi++strated in the -o++oing e0a&p+es:
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 6/43
Lect 23 P. 6
De!+aring Str!tres (str!t)
Reser5es Spa!e
str!t
&$9e0a&p+e
int +a,e+;
!har +etter;!harna&e<2=>;
? &$str!t ;
Does Not Reserve Space
struct my_example
{
int label;
char letter ;
char name[20];
} ;
/* The name "my_example" is
called a structure tag */
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 7/43
Lect 23 P. 7
User Dened Data T$pes(t$pede-) The C +angage pro5ides a -a!i+it$ !a++ed typedef -or
!reating s$non$&s -or pre5ios+$ dened data t$pena&es. @or e0a&p+e the de!+aration:
t$pede- int ength;
&aBes the na&e Length a s$non$& (or a+ias) -or thedata t$pe int .
The data t$pe na&e Length !an no ,e sed inde!+arations in e0a!t+$ the sa&e a$ that the datat$pe int !an ,e sed:
ength a , +en ;
ength n&,ers<1=> ;
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 8/43
Lect 23 P. 8
T$pede- Str!t
O-ten t ypedef is sed in !o&,ination ith struct tode!+are a s$non$& (or an a+ias) -or a str!tre:
t$pede- str!t FG Dene a str!tre GF
int +a,e+ ;
!har +etter;
!har na&e<2=> ;
? So&e9na&e ; FG The a+ias is So&e9na&e GF
So&e9na&e &$str!t ; FG Create a str!t 5aria,+e GF
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 9/43
Lect 23 P. 9
A!!essing Str!t e&,ers
Indi5ida+ &e&,ers o- a struct 5aria,+e &a$ ,ea!!essed sing the str!tre &e&,er operator (thedot .):
&$str!t.+etter ;
Or i- a pointer to the struct has ,een de!+ared andinitia+ied
So&e9na&e G&$ptr J &$str!t ;
,$ sing the str!tre pointer operator (the KL): &$ptr KL +etter ;
hi!h !o+d a+so ,e ritten as:
(G&$ptr).+etter ;
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 10/43
Lect 23 P. 10
Sa&p+e Progra& 'ithStr!tsFG This progra& i++strates !reating str!ts and then
de!+aring and sing str!t 5aria,+es. Note thatstr!t persona+ is an in!+ded data t$pe in str!tidentit$.GF
Min!+de stdio.hLstr!t persona+ FFCreate a str!t ,t dont reser5e spa!e.
+ong id;
4oat gpa;
? ;
str!t identit$ FFCreate a se!ond str!t that in!+des therst one.
!har na&e<"=>;
str!t persona+ person;
? ;
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 11/43
Lect 23 P. 11
Sa&p+e Progra& 'ith Str!ts (!ont.)
int &ain ( )
str!t identit$ 6s J oe S&ith? Gptr J 6s ;
6s.person.id J 12"#Q7 ;
6s.person.gpa J ".# ;
print- (s +d -Vn 6s.na&e 6s.person.id
6s.person.gpa) ;
print- (s +d -Vn ptrKLna&e ptrKLperson.id
ptrKLperson.gpa) ;
?
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 12/43
Prpose o- str!tres
Computing applications often
need data of different kinds to be
associated. A typical place wherethis occurs is in a record . For
example a database record for a particular student might contain
the student number full name
course!code and ear of entr .
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 13/43
Re!ord de!+aration
str!t stdent
!har stdno<12>; !har na&e<Q=>;
!har !orse<7>; int start$ear;?;
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 14/43
ore e0a&p+es
str!t !oord 4oat 0$; ?;str!t !o&p+e0 4oatrea+i&aginar$; ?;
"he data might ha#e different
types but does not ha#e to be ofdifferent types. Structures also
help with the naming of related
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 15/43
A++o!ating &e&or$ tore!ords
"he abo#e declarations don%t create any space for data
storage but they pro#ide a template for data of the
appropriate types to be allocated. &et%s allocate somestorage to data #ariables'
str!t !oord origin !rsor;
FG 2 0K$ !oordinates !a++ed origin and!rsor GFstr!t stdent sdn9$ear2<Q=>;
FG arra$ -or Q= stdents GF
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 16/43
Dening a ne data t$pe
(etter to ha#e one word e.g.COORD to declare the type of data
than ) words e.g. struct coord . "his
needs a typedef statement'
str!t !oord 4oat 0$; ?;t$pede- str!t !oord COORD;FG COORD no sa&e as str!t !oord GF
COORD origin !rsor;
FG de!+are 2 0K$ !oordinates !a++ed origin and!rsor GF
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 17/43
Use o- the dot str!tre &e&,era!!ess operator
*embers x and y of origin and
cursor can now be accessed as
origin.x origin.y cursor.x and
cursor.y e.g.
origin.0J".Q;
origin.$J2.=;
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 18/43
Ha5ing dened a ne datat$pe
!such as float and int . "his
simplification is useful as thename of the data type is going to
appear in many places within our
program e.g. for declaring types
of local data within functions or
function parameters.
,t may not seem the case but
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 19/43
@n!tions hi!h !o&&ni!atesing str!tred para&eters
4oat distan!e(COORD a COORD ,)FG !a+!+ate distan!e ,eteen a and , GF
4oat 5erthori;FG J distan!e 5ert J $1 K $2 hori J 01 K 02 GF
horiJa.0 K ,.0; FG the horionta+ distan!e GF 5ertJa.$ K ,.$; FG the 5erti!a+ distan!e GF FG !a+!+ate as the h$potense o- a right ang+etriang+e GF Js*rt((horiGhori) 3 (5ertG5ert)); FG P$thagorstheore&: GF retrn ; FG G J 0G0 3 $G$
GF?
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 20/43
@n!tions hi!h !o&&ni!atesing str!tred retrn 5a+esCOORD get!oord(5oid)
FG noteretrned t$pe is COORD GF
FG pro&pts -or and
retrns a !oordinate GF COORD te&p;
print-(Enter 0 and $!oordinates Vn); s!an-(-
-te&p.0te&p.$);
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 21/43
A progra& sing distan!e andget!oord part 1
Min!+de stdio.hLMin!+de &ath.hL
FG needed to se s*rt() s*are root GF str!t !oord 4oat 0$; ?;
FG de!+are str!tre !oord as ha5ing 0 and $ &e&,ersGFt$pede- str!t !oord COORD;
FG COORD is no sa&e as str!t !oord GFCOORD origin !rsor;
FG de!+are 2 0K$ !oordinates !a++ed origin and !rsor GF
FG -n!tion protot$pes GF4oat distan!e(COORD a COORD ,);COORD get!oord(5oid);
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 22/43
A progra& sing distan!e andget!oord part 2
int &ain(5oid) COORD origin !rsor; 4oat separation;
print-(enter detai+s -or origin:Vn); originJget!oord();print-(enter detai+s -or !rsor:Vn);
!rsorJget!oord();
separationJdistan!e(origin!rsor); print-(the distan!e ,eteen originand !rsor is -Vnseparation); retrn =;
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 23/43
Use o- str!tre pointers*embers x and y of coordinates a
and b can also be accessed
through pointers to a and b so
that if pointer p stores the addressof a' p=&a; then
p->x
directl accesses member x of a
e! ara ons a o5e ere
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 24/43
Rerite the a,o5e progra& singpointers
e! ara ons a o5e ereo- headers str!t !oord
and t$pede- COORD sa&eas ,e-ore so not repeatedGF4oat distan!e(COORD GaCOORD G,);
FG a and , are nopointers to COORD GF
5oid get!oord(COORD
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 25/43
Str!tres and arra$s-a#ing indi#idual names for / coordinates is #ery
awkward. +e can declare the data as an array'
t$pede- str!t !oord 4oat 0$; ? COORD;COORD graph<1=>;
+e can then assign members of our structure array like
this'
graph<>.0 J 12.Q; graph<>.$ J 7."; FGassign 5a+es to tenth e+e&ent <> o- grapharra$ GF
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 26/43
Or e !o+d inpt 5a+es into a++!oordinates sing a +oop
int i;-or(iJ=;i1=;i33) print-(Enter 0 5a+e -or !oordinatedVni31);
s!an-(-graph<i>.0); print-(Enter $ 5a+e -or !oordinatedVn
i31);s!an-(-graph<i>.$);
?
O i t t i t
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 27/43
Or sing str!tre pointernotation
int i;-or(iJ=;i1=;i33)
print-(Enter 0 5a+e -or !oordinatedVni31);
s!an-(-(graph3i)KL0);FG KL has higher pre!eden!e than GF
print-(Enter $ 5a+e -or !oordinatedVni31);s!an-(-(graph3i)KL$);
?
A t t t i i
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 28/43
A str!tre !ontaining anarra$
t$pede- str!t stdent !har na&e<"=>;
4oat &arB;? STUDENT;
STUDENT $o;print- (enter na&e and &arBVn);s!an- (s-$o.na&e$o.&arB);
FG $o.na&e is the address o- the na&e arra$ GFFG so no a&persand needed to get addressGFFG WUT $o.&arB is not an arra$ so needs GF
print- (he++o s $or &arB is -Vn$o.na&e$o.&arB);print- (The rst +etter o- $or na&e is !Vn$o.na&e<=>);
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 29/43
Assigning str!tres
Unlike arrays structures can be copied as a complete entity
using a single assignment. "his sa#es time. So instead of
writing'
str!p$(te&p.na&e$o.na&e);FG se -n!tion str!p$ to !op$ a string GF
te&p.&arB J $o.&arB;FG !an assign &arB in one go GF
we can write'
te&pJ$o; FG !op$ str!tre in one go GF
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 30/43
Pointer @nda&enta+s 'hen a 5aria,+e is dened
the !o&pi+er (+inBerF+oadera!ta++$) a++o!ates a rea+&e&or$ address -or the5aria,+e. int 0; i++ a++o!ate # ,$tes
in the &ain &e&or$ hi!hi++ ,e sed to store an integer5a+e.
'hen a 5a+e is assigned toa 5aria,+e the 5a+e isa!ta++$ p+a!ed to the&e&or$ that as a++o!ated.
0J"; i++ store integer " in
00000000
00000000
00000000
00000011
x
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 31/43
Pointers 'hen the 5a+e o- a 5aria,+e is
sed the !ontents in the&e&or$ are sed. $J0; i++ read the !ontents
in the # ,$tes o- &e&or$ andthen assign it to 5aria,+e $.
0 !an get the address o- 0.(re-eren!ing operator )
The address !an ,e passed to a-n!tion:
s!an-(d 0); The address !an a+so ,e stored
in a 5aria,+e XX
00000000
00000000
00000000
00000011
x
y
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 32/43
Pointers To de!+are a pointer 5aria,+e
t$pe G pointerna&e;
@or e0a&p+e: int G p1; p1 is a 5aria,+e that tends to point
to an integer (or p1 is a int pointer) !har Gp2;
nsigned int G p";
p1 J 0; FG Store the address in p1 GF s!an-(d p1); FG i.e. s!an-(d0); GF p2 J 0; FG 'i++ get arning &essage GF
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 33/43
Initia+iing Pointers iBe other 5aria,+es a+a$s initia+ie
pointers ,e-ore sing the&YYY @or e0a&p+e:
int &ain()
int 0;int Gp;
p J 0;s!an-(dp); FG Corre!t GF
?
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 34/43
Using Pointers Zo !an se pointers to a!!ess the 5a+es
o- other 5aria,+es i.e. the !ontents o- the&e&or$ -or other 5aria,+es.
To do this se the G operator
(dere-eren!ing operator). Depending on di/erent !onte0t G has
di/erent &eanings.
@or e0a&p+e:
int n &J" Gp;
pJ&;
nJGp;
print-(dVn n);
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 35/43
An E0a&p+eint &J" nJ1== Gp;
pJ&;
print-(& is dVnGp);
&33;
print-(no & is dVnGp);
pJn;
print-(n is dVnGp);
GpJQ==; FG Gp is at the +e-t o- J GFprint-(no n is dVn n);
3
4
100
500
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 36/43
int &ain() print-(Enter $or na&e ); s!an-(ssA.Na&e); print-(Enter $or ,a+an!e ); s!an-(+-sA.,a+an!e); print-(VnNa&e : s sA.Na&e); print-(+- sA.,a+an!e); get!h(); ?
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 37/43
Pointer Arith&eti! (1)
int a< 1= > Gp;
p J a<2>;
Gp J 1=;
G(p31) J 1=;
print-(d G(p3"));
int a< 1= > Gp;
a<2> J 1=;
a<"> J 1=;
print-(d a<Q>);
a a[0] a[1] a[2] a[3] a[4] a[] a[!] a[] a[#] a[$]
p p%1 p%2 p%3 p%4 p% p%! p%
&hen a p'inter (ariable p'ints t' an array element) there is a n'ti'n ' adding 'r
subtracting an integer t'/r'm the p'inter+elements
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 38/43
Pointer Arith&eti! (2) ore e0a&p+es:
int a<1=> Gp G*;
p J a<2>;
* J p 3 "; FG * points to a<Q> no GF
p J * [ 1; FG p points to a<#> no GFp33; FG p points to a<Q> no GF
pKK; FG p points to a<#> no GF
Gp J 12"; FG a<#> J 12" GF
G* J Gp; FG a<Q> J a<#> GF
* J p; FG * points to a<#> no GF
s!an-(d *) FG s!an-(d a<#>) GF
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 39/43
Pointer Arith&eti! (") I- to pointers point to e+e&ents o- a
sa&e arra$ then there are notions o-s,tra!tion and !o&parisons ,eteen theto pointers.
int a<1=> Gp G* i;
p J a<2>;
* J a<Q>;
i J * K p; FG i is "GF
i J p K *; FG i is K" GFa<2> J a<Q> J =;
i J Gp K G*; FG i J a<2> [ a<Q> GF
p *; FG tre GF
p JJ *; FG -a+se GF
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 40/43
Str!t
struct delcaration format
str!t tag str!treL
datat$pesL indentier1;
datat$pesL indentier2;
:
datat$pesL indentier";
?
struct sta/
char na&e<1Q>;int sta/9id;
float sa+ar$;char position<1=>;
?
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 41/43
I++stration o- str!t storage
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 42/43
E0a&p+e
1. 'rite a re!ord o- a na&e !onsists o- data: rstNa&e andse!ondNa&e.
2. 'rite a progra& seg&ent to a++o ser to enter the inpt -orea!h e+d o- the str!t in (1).
". 'rite a re!ord o- a detai+ !onsists o- data: position sta-Id and
sa+ar$.
#. 'rite a progra& seg&ent to a++o ser to enter the inpt -orea!h e+d o- the str!t (2).
Q. 'rite a re!ord o- a orBerIn-o sing str!t &e!hanis&. There!ord !onsists o- na&e and detai+ here:
i. the na&e !onsists o- the -o++oing data: rstNa&e and se!ndNa&e.
ii. the detai+ !onsists o- the -o++oing data: position sta-Idand sa+ar$.
. 'rite a progra& seg&ent to a++o ser to enter the inpt -orea!h e+d o- the str!t.
7/21/2019 07 BIT 10303.pptx
http://slidepdf.com/reader/full/07-bit-10303pptx 43/43
Ne0t Chapter :
FILE
.1 The sage o- +e in Cprogra&&ing -or inpt and to
prod!e otpt or report