C++ Pointers PDF
C++ Pointers PDF
C++Pointers
C++POINTERS
http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm
Copyrighttutorialspoint.com
C++pointersareeasyandfuntolearn.SomeC++tasksareperformedmoreeasilywithpointers,andotherC++tasks,suchasdynamic
memoryallocation,cannotbeperformedwithoutthem.
Asyouknoweveryvariableisamemorylocationandeverymemorylocationhasitsaddressdefinedwhichcanbeaccessedusingampersand &
operatorwhichdenotesanaddressinmemory.Considerthefollowingwhichwillprinttheaddressofthevariablesdefined:
#include<iostream>
usingnamespacestd;
intmain()
{
intvar1;
charvar2[10];
cout<<"Addressofvar1variable:";
cout<<&var1<<endl;
cout<<"Addressofvar2variable:";
cout<<&var2<<endl;
return0;
}
Whentheabovecodeiscompiledandexecuted,itproducesresultsomethingasfollows:
Addressofvar1variable:0xbfebd5c0
Addressofvar2variable:0xbfebd5b6
WhatArePointers?
http://www.tutorialspoint.com/cgibin/printpage.cgi
1/4
7/22/2015
C++Pointers
Apointerisavariablewhosevalueistheaddressofanothervariable.Likeanyvariableorconstant,youmustdeclareapointerbeforeyoucan
workwithit.Thegeneralformofapointervariabledeclarationis:
type*varname;
Here,typeisthepointer'sbasetypeitmustbeavalidC++typeandvarnameisthenameofthepointervariable.Theasteriskyouusedto
declareapointeristhesameasteriskthatyouuseformultiplication.However,inthisstatementtheasteriskisbeingusedtodesignatea
variableasapointer.Followingarethevalidpointerdeclaration:
int*ip;//pointertoaninteger
double*dp;//pointertoadouble
float*fp;//pointertoafloat
char*ch//pointertocharacter
Theactualdatatypeofthevalueofallpointers,whetherinteger,float,character,orotherwise,isthesame,alonghexadecimalnumberthat
representsamemoryaddress.Theonlydifferencebetweenpointersofdifferentdatatypesisthedatatypeofthevariableorconstantthatthe
pointerpointsto.
UsingPointersinC++:
Therearefewimportantoperations,whichwewilldowiththepointersveryfrequently.a wedefineapointervariablesb assigntheaddressofa
variabletoapointerandc finallyaccessthevalueattheaddressavailableinthepointervariable.Thisisdonebyusingunaryoperator*that
returnsthevalueofthevariablelocatedattheaddressspecifiedbyitsoperand.Followingexamplemakesuseoftheseoperations:
#include<iostream>
usingnamespacestd;
intmain()
{
intvar=20;//actualvariabledeclaration.
int*ip;//pointervariable
ip=&var;//storeaddressofvarinpointervariable
cout<<"Valueofvarvariable:";
http://www.tutorialspoint.com/cgibin/printpage.cgi
2/4
7/22/2015
C++Pointers
cout<<var<<endl;
//printtheaddressstoredinippointervariable
cout<<"Addressstoredinipvariable:";
cout<<ip<<endl;
//accessthevalueattheaddressavailableinpointer
cout<<"Valueof*ipvariable:";
cout<<*ip<<endl;
return0;
}
Whentheabovecodeiscompiledandexecuted,itproducesresultsomethingasfollows:
Valueofvarvariable:20
Addressstoredinipvariable:0xbfc601ac
Valueof*ipvariable:20
C++PointersinDetail:
PointershavemanybuteasyconceptsandtheyareveryimportanttoC++programming.Therearefollowingfewimportantpointerconcepts
whichshouldbecleartoaC++programmer:
Concept
Description
C++NullPointers
C++supportsnullpointer,whichisaconstantwithavalueofzerodefinedin
severalstandardlibraries.
C++pointerarithmetic
Therearefourarithmeticoperatorsthatcanbeusedonpointers:++,,+,
Thereisacloserelationshipbetweenpointersandarrays.Letuscheckhow?
http://www.tutorialspoint.com/cgibin/printpage.cgi
3/4
7/22/2015
C++Pointers
C++pointersvsarrays
C++arrayofpointers
C++pointertopointer
Youcandefinearraystoholdanumberofpointers.
C++allowsyoutohavepointeronapointerandsoon.
Passingpointerstofunctions
Passinganargumentbyreferenceorbyaddressbothenablethepassedargument
tobechangedinthecallingfunctionbythecalledfunction.
Returnpointerfromfunctions
C++allowsafunctiontoreturnapointertolocalvariable,staticvariableand
dynamicallyallocatedmemoryaswell.
http://www.tutorialspoint.com/cgibin/printpage.cgi
4/4