























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of pointers in c++ programming, explaining their role in sharing information between code sections, enabling complex data structures like linked lists, and their relationship with arrays. Topics covered include pointer declaration, initialization, memory addresses, pointer operators, and pointer arithmetic.
Typology: Slides
1 / 31
This page cannot be seen from the preview
Don't miss anything!
























int x=5;int *xptr;
5 x of type int 0x0012F690^ 0x0012F
xptr of type int*
int x=5;int *xptr;xptr=&x; //points to x
5 x of type int 0x0012F
xptr of type int*
int x=5;int *xptr;xptr=&x; //points to x
x of type int^5 xptr of type int*
Pointer OperatorsThere are two special operators that are usedwith pointers: * and &The & is a unary operator that returns thememory address of its operand.int balance = 350;int *balptr;balptr = &balance;
The second operator * is the complement of &.It is a unary operator that returns the value ofvariable located at address specified by itsoperand.int balance = 350;int *balptr;balptr = &balance;int value;value = *balptr;
//what does value contain? Pointer Operators
PointersWhen a pointer is first allocated it does notpoint to anything.Trying to de-reference an un-initialized pointeris a serious runtime error.If you are lucky the dereference will crash orhalt immediately.If you are unlucky it will corrupt a random areaof memory – so that things go wrong after someindefinite time.
Pointers Make a memory drawing!
int p;int x=12;p=&x;cout<<x<<endl;//Assign a value to the location pointed to by pp = 101;cout<<x<<endl;
//what is value of x? Assigning values through a pointercout<<*p<<endl;cout<<p<<endl;cout<<&x<<endl;
int *p;int x=12;p=&x;cout<<x<<endl;
//prints 12 //Assign a value to the location pointed to by p*p = 101;cout<<x<<endl;
//prints 101 cout<<*p<<endl;
//prints 101 cout<<p<<endl;
//prints 0012FF cout<<&x<<endl;
//prints 0012FF Assigning values through a pointer(Note memory address may be different when you run it on your machine)