/ Ac'cessing structure mernbers " 1n erder to' ass'vgn a value to a structure member, the'dot () (9r MeMber Eccess eDeretot) Iin les eech men.bef riam,e .,-,t" tHeePerater sttucrure varieble ' -tntlude
Assignment of stfuctu're variables g 9 The assbgnment epereLpr (=} cepies elI steucture membeFs Vo inether structure veriable if theLr structute tegs are s-me Such an s,gnmethL ,s d/fferent frem arrays -Snclude (std h) "lntlude csttln h) b=a mane thGN- ,b "thrlo), P"irti{" "t:,:,t7;.:ut`di:?:ti p,:::iE: .,.:?:: ve,ts::;e sc,.2g: lht pnye retui"e k, tnt pt.Cvoid]{ prinLl(- pr-uLl(" prtntt{" go ss XsXp' nane) ldNn' maLh) td"t' phye) Prepk go t '-7T 1 N ' (" ・v ' o !Spti !
A'ss';gnrnent of structuie variables a The essbgnment gpareLor (=#} cepies alI structure members to another struqture vertable ;f theLt strucTure tegs are seme' " SucH en ass,gnmefiL ,s dLfferent frem erreys "=ntlvde (stdle b) #Snclbde cstrlng h) b=q pr;ntflh lta"- ZeNH'.b thamdi), strucl sLndent{ pvinctC- p=int[<・・ phy:r.: vetl:11::.;hny,Wl.l: qhar nanq[2e]. irt inath, tht ptyst retura Ot l:, int nd7Cvoia){ } svzauut utudent a,b, [ou[puz] scdeiy{m aanet 'Ftank')', Name rra=k amatllFYO, Math go qphVS=SS, phyrits SS Name rzqnk prhtLt(" Npmp nsXn',E nan-), bleuh eO pr-uLl(" M"th adNn',Z MaLIO phytte.e sl Fhye-cs AdVt' ," phy#) PlllLtS{" × xxK l u ' Ns J ' n e ly- ' !
Rointer to structure' " We can hawe e pe,nter tb a sinEle structure veria'ble e igueh s ,oointer pbints to tHe eeldress eF 1,.st member ef tHfi! s[ructure a //// /. // /1 sb=ucL studenL{ Po/nt¢r Allocated to char nblne [2o], eneme -nt; math. =pc phys, -5'EiEIE- },, ep ys pt meMgwe)d)ff sVructs ,student a. "pF, The layout of tha cermputer niemory, /]) be sbove x tt 1 ' t "'i" ' i ' ' ,!S ' / ' / ' f ii
Reinter " " a StrucL stuaenL{ pt]ev nithla' waO], 'xht math. =nt phye, }t nt,malrL(vo=d){ /t $t=uct ,$tudertt. a. Spa. Pe - in, ' The leyout of the ."mp)uter niemery w/11 to struCtuidi We can have a po,nter to a singie structure variable Sugh s nointet pcrints te Vhe address oF -/ kst mem'ber ef the s[ructute The "&'' eperEter retunts the eddress ef the sttucture variable z Pa/ntet 'Pa Allecatedto eneme ff MbY be above 1 " 5. / g ta -V ejiit5 /
Ac'cessing structure members with pointer e We use arrew (->) te eccess stiuctuve metnbers whert we have e' structutg Usage bs name-o[pot"ter->membeF-n)me ." T+IFs meams {*name.er-pointet) member-nciFneEs weil Hti]Ll-Ne {stdio h) #Tncluae stnueL qhur tpt tnt }r inpt tsLl:ng h) etuaenti[ htrnye[no] Mitt]li phys. mdith{vpldM etruc= #tuae sLrepVC-nane" e m4Lh = gOt a rhys - aS, Prtlltt(" prlmtfC" pTlt]LIC. .-Pd. ttpt tt) btane lsX Le 'wD Math ZdLn u mdzlT) Phys-c! JdXn'' d phys) } parathT sLrqpyrpa-nemo. pa-bphys = su Vtt"LtC PT-TtfC" pxtnLIC reLutn O, (O"LFUV) lieilSter ef ttlhoindi-)1 ptaneXSim"tPE-)nemo) "o mh laXn".pe-)tvdrliO , oliys=cslaNn"tl)aJpPhys) N x・ "i ge 1 / / t , ' 1 11 t s ` " ' -
' Exarnple: - Cens).Udr eh'#'gu Vpt]L 6f tiinsgentlpwhildig pEegrim d;include Estd-e,h> #lqC] udq tslring h) .etfue= seutien=C char nank[2Q], int mEth. int pbys, v =nt meinCyatd){ struov stuotorvt e"pp, sLrcpy(a narno "trr"nk-}, or muth = ee, a pbys = SS, p'rzntiC" Nnme ZgNn",i v"inE), p=irttt(" Mpth gdNn",lt watlt) , prt"tt( Phyeics 14Nn ,a ?byE),, PmYzaa, etrapy(pn-7ualne, 'Tllomas") nn-}vhys - 9V } PIXacst- Vr-nti!tt, pr±nti" printtt- p=intt{ PT-hvfSM tabUT= O [OtlLP"t] ' NtuDe Halth Pltysme Nnme Mqth mirysics Xaln",pa.}nane), 4dXll// pe-bmztlll. ZdNM" pa-)plTysO 4Gt-H.dTthe)t paNn- e math), ZdNv/'.avtLys), ` -/ 't ' / !g - y ' //
o 'Sornnple program' using'the nptnter, as functien erguments #inq]uata gstdte lp -nt M"inCvb-d)( eCrUCt tthldGnt{ kOecltting sLructbrq-1 fitWCe =tUde"t Sl - [1/F=ankV cl:ur na]/,a [ao], Avotnge(mm) =nt mat;h, pL!nttC, ptpte =ZaN=" 1 name) int phyfi, pr4"tt(""ath .adNn" tdhlih) gpmbLe ave, prhLr("Phye FZdX=" phyE)- }, pr!=nt('Avo uX alXn' pve) Y:t:tS::i"::(:#.r".-:-t..:,t"..disgull:. .:::it } ienu=n o, -mp Eun, / / sun F B=dL)math + stdJ>ptlvg, iLe!=eve = [daulilel sumtza, ' , 1 I go SSI "Nk thaor k / ' / ' L :sd 1 ' ' '
' to Samplle progerath uEiHgethe noin'ter as functien ergunnents "-nq"zde cqtine hn Zct -al-CVe-d){ eCrVct SttidGnt{ VMe.cltrin.s s.=rucen.e-i StrUCt StOdenV St - ["Vlmpk",Qe,SS} ,chthrnmume[2o" AVstdge(tM) =nt mpth pitntt('Norme =ZeN=" Sl nnm") int phys, pr4=tt(""ach =aaXn' ,Sl tuuth), qpuble uvq, pttuttCNPhye FXdXn",tSl phye) i, p]tn#t<'Avo =ti 2tXn',Sl pvq). 1ve;'di.t:;:iti:: Cr:-gy.:; st:;ik-k.;::it } Tec"rn o・ "t e"n. 1,1 sun F B=dL)mat]1 + std-}plivs, sedsi>evri = cdthtsblel sunt2, 1/ , ' g v 1
/ a Sample program us.inge the podnter as fvnctien eFgumenTs it±nqlude getdio l) =nt thtSltCvb-d)t e=-Uct skuelent{ di-neaaring stnict"=ev StrUFt gtudent Sl g ["ltank".,eO."S} -t ckhT nmu=gt2'op" Aver"sECmsl), ttit tuabh prlnttC' Nane aitiNta",Sl nomo), ,thtt phyfi, prz=tt(""ach naaXn' ,St tb{Lhl': double tvq, prtnLtC''Phye =ZdN"",Sl phys) }, p] tntt<'ltvo rrt 2fXn',Sl qve), ]ettlrn O, vvid AyettiapCBt=uci studettL +std) khPeimer nz linch;o'" o=tiume=t.s-1 ) ==t eun ipt F std-)math + s"d->pLopg, = Cdieuble) eun std-pthve t? , ' "u g' J pt / .rf- / x r 1 - '
` sihnnplk program using th'e po:nter es functien erguments "inglude tstdzo h) ±nt maSbCve-d){ eterdet StUtlenC{ kng'elu=ing structthTe-1 SVrUtt FtPdent S± - {//F=anltV ' Ehhr name[2Pl/ int tastfi, =nt phyfi /aptible uve, M ve=d thvettissC4t=uct stndient -tsvd){ 1-Poimter u- lumct-e" eTgnmeb-..+/ -#t mvm :'ge..;.::d:'?itth..l.#":;:i:T"' }- } Aver"gsCmst), ?Ltntt('Nanti prt=t±C"Hath prt-ttC''Phy4 prtntt('Avo 1eLurn O tovtppq Nhme =Ftank Mtith hee ' Phye rpB3 AvH =sG se =XsNn'/,st -kdSn',sl FXaXn",Sl ltZ 2iXn', ' 90 USI ntsme), lmltLh)/, phys)' St ave),) P . ri " x x × vtt 1 -
-/ $VlitctuFes as the retrurn value ef function " Str-ctures can be used es the teturn ve]ue ef functians " The ''Average" function im previous page is rewr)tLen es foIiows Nete ehst the perameter of the function is alse the struot"re variable strtct mLudent AverageCstrucL sLudent !emp){ Veinp ave = (double) (tamp matli + temp phvs)12, trFezurn temp, ) Lmb mam(vo=d){ Sl=Average(Sl). . . es Y ", f t T EX "ts et x
Stfuctures as the, return value of function " StruCtures can' tsb-used zs the rUturn value ef funatienS g The ''Averege" function in previous pegE is rewritten es foIlode that the petarnetet of the function ts also tlie structure veriable struct BLudent Av- age(st=ucL sLudent LempD{ eemp orve = (doub -) (temp math + temp pbysDV2. retuvn bemp, ) in'tt ma.-p(vo=a){ S±=AeerageCSt). r/ t 1 : Nete ' i' `(i ' Sr " ' ,g y l r '
Array of structu"e ' " We can also declsre en, znraylof stfvcture vefiel)les Each element af Lhe artay represe"ts n structvke veriabie Lncliwding ell memba"i of the struCtLdre st=u=C strnctu=e Lag ]aine-et.a=ray[#.e±"eleme]ts], 'o Decler2tien and tisege are the seme es the array st=uct sLude]L Std[201, ior(i!cr,1dl,-++){ Std[=] nyve ; '(tlquble),(.Std [-1 } rnakb-SFd[il phpts)IR, "- .J. . 1 pu >.stgl -L< ' ' / ' v x 1
A.rnay mT structniPe ' e We can a-se dieclere en, array efstrvcture vnriables Eech'eletnent af the arrzy fe'ptesents e structL,rfe verreble Lncluding ell mEm'bErs ef the ptrUCtLdre st::ucV Stracture Lag naneJet.nrray[#ro:"elements], 'o Dgciera.t:en ettd usege are rhe seme as the arreV et=gct sbuaent S t[20], io=(==O,1
Array of structta-e " We can also declare an array o'f structure varisl)Ies Each element af ::hreu:tLtarYe rePreSe"tS e st"ucture variabie /ndtsding ell members ef the c, st=uct sttuotu=e Lag -ame-etntray[#.b±"e16ments] , iJ Decletatibn end LTs.age are The same Es the srrey str'uct eLudEnt Std[20], iorCL!a , ,-
S"ummary o Torter/al of sttuctures m C - Usage er structures o Hovv Lo :nitiarize e strLtcture vac/mble mnd eccess inernbers - Po:nters to the structure var[ablle o Return e structure vat!able o Arrmy e'f structude ' - [ 1 tdi ' I t ' " // / :