Pointers: Advanced Programming Language: Pointers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

AdvancedProgrammingLanguage:Pointers

Pointers
isthememoryaddressofavariable.

Syntax:

Type_Name*Pointer_1,*Pointer_2,...,*Pojnter_n;

Declaringapointerisjustlikedeclaringanordinaryvariableofacertaintype.
int*p1,*p2,v1,v2;
p1andp2arepointers
v1andv2arevariable

Table:declaringpointervariable
Declaration Description
int*p;
pointerthatpointstoavariableoftypeint
float*p;
pointerthatpointstoavariableoftypefloat
double*p;
pointerthatpointstoavariableoftypedouble
MyType*p; pointerthatpointstoavariableoftypeMyTypeClass

&symbol

usedtodeterminetheaddressofavariable.

int*p,v;
//setthevariablepequaltoapointerthatpointstothevariablev
p=&v;
*p=10;//Dereference

Output
SourceCode:

RMDA

Page1

AdvancedProgrammingLanguage:Pointers

The*operatorinfrontofapointervariableproducesthevariabletowhichitpoints.Whenusedthis
way,the*operatoriscalledthedereferencingoperator.

The&infrontofanordinaryvariableproducestheaddressofthatvariable;thatis,itproducesapointer
thatpointstothevariable.The&operatorissimplycalledtheaddressofoperator.

Useofassignmentoperatorwithpointervariables

after:
before:
p1=p2;

*p1=*p2;

before:

after:

RMDA

Page2

AdvancedProgrammingLanguage:Pointers

SourceCode:

Output:

Illustration:

RMDA

Page3

AdvancedProgrammingLanguage:Pointers

new

Sinceapointercanbeusedtorefertoavariable,yourprogramcanmanipulatevariablesevenif
thevariableshavenoidentifierstonamethem.

Thenewoperatorcreatesanewdynamicvariableofaspecifiedtypeandreturnsapointerthat
pointstothisnewvariable.

example:

p1=newint;

Thisnew,namelessvariablecanbereferredtoas*p1(thatis,asthevariablepointedbyp1)

SourceCode:

Output:

RMDA

Page4

AdvancedProgrammingLanguage:Pointers

Illustration:

heap(freestore)

Itisaspecialareaofmemorythatisreservedfordynamicallyallocatedvariables.Anynew
dynamicvariablecreatedbyaprogramconsumessomeofthememoryinthefreestore.Iftherewas
insufficientavailablememorytocreatethenewvariable,thennewreturnedaspecialvaluenamed
NULL.

delete

operatoreliminatesadynamicvariableandreturnsthememorythatthedynamicvariable
occupiedtothefreestoremanagersothatthememorycanbereused.Whenyouapplydeletetoa
pointervariable,thedynamicvariabletowhichitispointingisdestroyed.Atthatpoint,thevalueofthe
pointervariableisundefined,whichmeansthatyoudonotknowwhereitispointing.Ifpointervariable
pointingtothedynamicvariablethatwasdestroyedandbecomesundefinediscalleddanglingpointers.

TypeDefinitions

Youcanassignanamedefinitionandthenusethetypenametodeclarevariablesusingtypedef
keyword.

Syntax:

typedefknown_type_definitionNew_Type_Name;

RMDA

Page5

AdvancedProgrammingLanguage:Pointers

SourceCode:

Output:

PointersasCallbyValue

SourceCode:

Output:

RMDA

Page6

AdvancedProgrammingLanguage:Pointers

DynamicArrays(Dynamicallyallocatedarray)

Isanarraywhosesizeisnotspecifiedwhenyouwritetheprogram,butisdeterminedwhilethe
programisrunning.

inta[10];
int*p;

legal:
p=a;
illegal:

a=p;

anarrayvariableisnotoftypeint*,butitstypeisaconstversionofint*.Anarrayvariable,like
a,isapointervariablewiththemodifierconst,whichmeansthatitsvaluecannotbechanged.

SourceCode:
Output:

RMDA

Page7

AdvancedProgrammingLanguage:Pointers

SourceCode:

RMDA

Page8

AdvancedProgrammingLanguage:Pointers

Output:
Case1:

Case2:

SourceCode:

Output:

RMDA

Page9

AdvancedProgrammingLanguage:Pointers

PointerArithmetic

typedefdouble*DoublePtr
DoublePtrd;
d=newdouble[10];

dcontainstheaddressoftheindexedvariabled[0].Theexpressiond+1evaluatestotheaddressof
d[1],d+2istheaddressofd[2],andsoforth.

for(inti=0;i<arraysize;i++)

cout<<*(d+1)<<;

isequivalentto

for(inti=0;i<arraysize;i++)

cout<<d[i]<<;

SourceCode:
Output:

RMDA

Page10

AdvancedProgrammingLanguage:Pointers

MultidimensionalArray

Output:

RMDA

Page11

AdvancedProgrammingLanguage:Pointers

RaggedArray

Isamultidimensionalarrayinwhichtherowshavedifferentlengthsoranarrayofpointers
whoseelementsareusedtopointtoarraysofvaryingsizes.

Example
SourceCode:

Output:

Discussion:

Theidentifieraisanarray,eachofwhoseelementsisanarrayof15chars.Thusa[0]anda[1]
arearraysof15chars.Theidentifierpisaonedimensionalarrayofpointerstochar.Theelementp[0]is
initializedtopointatabc:,astringthatrequiresspacefor5chars.Theelementp[1]isinitializedto
pointataisforapple,astringthatrequiresspacefor15chars,includingthenull\0attheendofthe
string.Thuspdoesitsworkinlessspacethana.

RMDA

Page12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy