Sunteți pe pagina 1din 4

7/22/2015

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

S-ar putea să vă placă și