0% found this document useful (0 votes)
3 views

The Memory Address

Uploaded by

Rajeev Shrestha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
3 views

The Memory Address

Uploaded by

Rajeev Shrestha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 16
ports ak 1 Deffne a pofnter wTth 78 syntax. List out the features of Pointers: A Programm pofater fo programmrng, fsa stores the memory address varfable +hat ft allows fndfrect QJ another varfaple - access +0 the value OV data stored at that memory toca tfon Syntax a Deciaie data_type *po?nrer_-name © address o} HASSIGN + 1 pofnter-name = Fvarfaple ; 7 y : fat num = 42: fn+PoPorer = ~num; Access yosue 40 the 5 pot value = *pomnter_name Byample = gar fos value = *fntPoPnter; fy the value ecinted ¥ pofhrer_name = name_new-vatue! “ExOmplet *fhtPofntey = 99; . Ie . ' atures: Oo; po?nters : : of : uy | Saves _memory Space + Pofnters save space by Storfng addresses fnstead of valves, Conserv?ng, memory. "| Faster fyecuHon Trme «_ (Pofnters offer qufcker access to memory. bypa- Ssfag Name lookups: We) Cer Rent memory Access |Pofniers enhance memory access eftPcfency, fdead for arrays and structures: | | My | Pyname memory Allocatfon | Pofhiers enable changing Memory allocatfon durfog runtime , useful “when needs vary. ve Use with data structure Pofnreres afd fn data. _structore Man vputatfon, \?ke Ifnted lestS-and trees. 2: WUrPte Short notes on the address C¥¢) and frnd?rectfon C*) operators The Address operator CL) TRe address operator Ps Unary operator frat retunes the address Of PFS operand. TRe ‘operand can be varfaple, an array element, Ora functfon. The address operator #s often Used to pass arguments by reference , to access arry elements, or to Aynamfcally allocate memory: ‘The Indfratfon operator C*) iTRe fndfrectfon operator fs unary operator that Ireturns the valve Of the opfect to PKS opera- Ind pofnts. The operand must bea pofnter | varfavie -Tne fnafrectfon operator fs Often value Of a varFable Fhrough used +0 access a pofnter , or 40 dereference a pofnter ail ee 4 Explafn cai by vaiue and call by reference WF an example. > Cau by value and Call by reference are tW00 Methods of passfng arguments to functfons (5 Cay by valve: Call by value means thak the | value of the argument fs Coptd fnto the parameter. Any changes made to the param- eter rhsfde the functPon do NOt AFFect tne orfginad Arg: *Here fs an example 0g Cal by value: -vofa swap Cfhra ,ftod¢ fat temp? temp = as ab b= +emp; fat mafn Og | fnt A= Loe y=20; | /£€0\) the swap qo , pass Swap lays lap > value of FY | iprfnte Ca = ANY, X)s |prrnre (hy > %~ANMS yds ‘yeturn 0; i ae es (Cain by reference t /Means that tne address of tne argumenr Ps | Passed Fnto tne parameter, Any changes (Made to the parameter fnsfae the functfon |OFFect tne orfgfna) argument: Here Ps an example of call by reference > Vora swap Cfnt %a, fat *d)& | fot temps temp = Fos Fat mafn tis Pat A=lo Y= 205 |? ass the addresses of Swap Carty): eos prfneg CM x= Zdlors X)y PrentpC'y= Zio", yd, return 03 2 ° 3 In generas, call by reference fF + all by value, bec yfno tne valu by reference use Pt alle 4fPociude +frnciude vord mafod) & fat a,*p; GMES CTC )5 Prfnt fc" Tne address of afs %U\NY, FQ )s prihtf( THe address of p PS 7% UIN", Fp); P=$a5 prPntfc" The address of A US?no pofnter fs PUNY PIS Bat *, | prPnteC' The address of A After Pacrement of pornter Ps YUAN” ,P)s Prfnrf(" Enter the Value of a tObe Btoredln); SCAN FCN Md) Fas prfnréc'\ The value of ats %d\n",a); printf ('The value of a usfng’ oot Red | M0", BE AC20) |) PrNIFC” The value of a using pofoter Fs ao) eetchc): 5 “The address of a fs 65524 The Address of p fs 65522 The address of a using pomter = 65524 THe address of a agter Fncremend fs 65526 Enter the value of & to be stored 4o4 TRe vaive of a Ps 404 The vawe of a usfng ¥ operator fs 104 The vaive of a pofnrer ?s 0. 2 The "L" operator , when used With pofnters , Allows Us to retrfeve the Memory Jocatfon / address 0g a varfaple. when combined WF the "¥)' Operator (deference dereference operaror). P led leks us access the valve Sfored at thar memory address - (RE )= that pofnter mahfpulott Pres carer mer J stonm ) *+ WAP to cae cPeperent ar?thmetfe operatfons (AS addfifon, subtractfon ¢ MUliPpfearPon usthg Pofnters | H¥aclude estdfo:n> vod performOp C fat a, Pn+b, Tht *sum, fot “PPE, fo+ * Prod ) § | *sum= Atos * | *dPep = a-b: ALS 2 SO Pte. ten aa * prod =a 5 3 *e? * V fot math cg Fat n3,n2,sum, arte. Prod; Prfnte CYEMter two numbers +» "), SCANFC nd Vd"s $NL,FN2), performOp (ni,n2, Ssum, saree 4 prod): Prfarf CVSuM? %AIn"), sum), printf CY DPFFerence = ¥d\nY Ppl printf C“Product td eo eas return 0; is Ps 4 | Obfectfue +2 2:1 = Pofprer wth one-dafmensfona) array | Code: | Hfhclude | fnclude | VORA maPn () |S frnt a 053=$1,2,3,4,5 44 PS | for CP= 0, F624 7 Prt] } 3 betcnc) ; | $ | Oorput ! Enter no-vaive of Adaress of Elements - = QloJ = 65933 Qf!1-65520 2 @e2)=- 68522 ac34= 65524 4 Q@C43- 6s526 5 | printhcu Element no- value of Address oJ 1-9) | | prfoiectalydI=l\kn Su 79e"y Rcat?), *Catt)); | | BAM Modtiiedy gan St Hea e cstafon> \# fncluge cconforn> votd mafne) | In+ fete number 2: fnr ats; |" Get the frie no from the user [prfotec' Enter the frie N02) [SCAN F CVG", BFPIeE-nuMber), ty PorHalfze the array wth 4ne numbers from Me fri. | for F=0: P55 THHS | 3 acra= Frle-number +i5 | 1.DRspiay tne content of the array using tne name | OF the avroy Pnseif as a pofnter | prfbtec'Lsing the name of the array Ptseie | as a pofpter :1n"); | “prPnte CY Enter no- valve OF Address of Bleminy:| idee CPO; Peed, FH IS prentpevatyarabBu 7d in F/AtPy *ca rt) oe 5 | |i OBplay ine content of array vsfng a Pofhicr) | fot *ptr=a, | ipinve crn Usfng a pofnrer # AN), | prfoth ("enter no - vave ef Address of Clem \0 : Lee — ————— — — sieges | FforC Peo} Pe=a 5 Pry PrfDHFCVACyAI=\en su valay, P, pirat, ¥Cprr +P): ¥ | 35 eH C)) 49 Output! ‘Enter the fPle number 3 do4 ntermo value Address Of lemenss: (07: 65514 404 C1- 65516 405 C2I1= 65518 4o6 €3)= ossz0 . 407% Ac43= 65522 4©3 sfng a pofnter: Enferno-value Address Of Elemrsents - Coy: 1436 -22 CL1= £439 0 C21~ 1940 oe C63= j4q2 0 C49= 9444 O ssfng the Name Of tne array Piselfas apotniey [3.42 Cau bY Value | [yp anciude | ly foclude | | yora SWAP C Fak x, Fa4y); yota Marni fat a= 80, b=90; prof’ valve OF A ana be before swapp?ng PE Yd “nd in "aib); eswapCarb); printf C" Value of aand od afier swapeng PS Fdlt XA\N4 arb); gerenO)y y vord SWaP (inata, Pat be int temp 4 demp=a}; azb, b= temp, 5 Output" vaive of cand b¢ befow swapprng Ps 30 20 value Of Gand od afrert swapprng Fs 30 20 i lo (9-22 BY Telcrengs 7 pfoclude ja fncivde ju fnciude Z2Stdlib-n> vota mafnt)¢ font *P: n=5, 75 Z | | pe(Pnt¥”) matioe CePzeoF irr) *n) » iprfntec “Enter Frve numpersin), |For CP=O; Pen; PtH < SCONE C7, Cpr Pl): y for cP=0; Pen ;Pmg Prin thts DIE’ AER) 1S 7 gerchcs, g | Botput: | Enter frve numbers. |4efaclude |4 fnclude ¢ StdiPb- h> fat mafnc) ¢ fnt n, *arr, ?,sum=0; PIPHEC Enter tne no. of maturad number : sant Curd', én): /lallocate memory Jor the array ‘ary = Cfnt¥) mahoc cn *sPee-of cPntJ); for CPO; Pen! pHs GQEF ATV EPI = PHL 5 fnd the Sum of the array elements for CP=0; PEN; TH), Sum+ = arrciy: | 1 Cadculate the average | Float aug = sum/n, yy | | | | Prfatece Sum of the first 7d naturalno Fs yen) Nsom), jetnte cu Tne average of me frrsk yd natural No- Ps re 2pin''s O1 avg) ; 5, return 0; 5 ee ourpur: Enter the number Ob frrst S Watural nO: 5 The sum 0b Ffrst S Natural no Ps LS The average Of the frst S naturods no Ps 3:00 | |

You might also like