Pointers
Introduction to Pointers in C++
Function Pointers are used to store address of functions, we’ve used pointers to point to data (variables)
Example
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int a = 10;
int *p;
p = &a;
cout << "Address of p is: " << p << endl;
cout << "Value of address p is : " << *p << endl;
cout << "Size of integer is : n = " << sizeof(int) << endl;
cout << "Address of p+1 is: " << p + 1 << endl;
}
Output
Address of p is: 0x7ffee1b4c60c
Value of address p is : 10
Size of integer is : n = 4
Address of p+1 is: 0x7ffee1b4c610
Working with Pointers
Pointers vs References
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int *ptr; // Create our Pointer
int var = 7;
int foo = 21;
cout << "prt: " << ptr << endl;
cout << "var: " << var << endl;
cout << "foo: " << foo << endl;
}
Output:
prt: 0x10e245025
var: 7
foo: 21
- Lets have ptr point to something !
Another Example:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int *ptr; // Create our Pointer
int var = 7;
int foo = 21;
ptr = &var;
cout << "prt: " << ptr << endl;
cout << "var: " << var << endl;
cout << "foo: " << foo << endl;
cout << "ptr value: " << *ptr << endl;
}
Output:
prt: 0x7ffee4997604
var: 7
foo: 21
ptr value: 7
Example:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int *ptr; // Create our Pointer
int var = 7;
int foo = 21;
ptr = &var;
ptr = &foo;
cout << "prt: " << ptr << endl;
cout << "var: " << var << endl;
cout << "foo: " << foo << endl;
cout << "ptr value: " << *ptr << endl;
}
Ouput:
prt: 0x7ffeee5a5600
var: 7
foo: 21
ptr value: 21
Now what about References?
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int *ptr; // Create our Pointer
int var = 7;
int foo = 21;
ptr = &var;
ptr = &foo;
int& ref = var;
cout << "prt: " << ptr << endl;
cout << "var: " << var << endl;
cout << "foo: " << foo << endl;
cout << "ptr value: " << *ptr << endl;
cout << "ref: " << ref << endl;
}
Output:
prt: 0x7ffee9295600
var: 7
foo: 21
ptr value: 21
ref: 7
Difference between Pass By Pointers and Pass By Pointer Reference
Example
#include <iostream>
using std::cout;
using std::endl;
int box1 = 1;
int box2 = 2;
int * gptr;
void passByPtr(int * ptr);
void passByPtrRef(int* & ptrRef); // Integar pointer reference
int main()
{
int *p = &box1;
gptr = &box2;
cout << "______________ Current Conditions _____________ " << endl;
if (p == &box1){
cout << "p is pointing to box1" << endl;
}
else if (p == &box2 ){
cout << "p is pointing to box2" << endl;
}
else{
cout << "p is pointing to another box " << endl;
}
if (gptr == &box1)
{
cout << "gptr is pointing to box1 " << endl;
}
else if (gptr == &box2)
{
cout << "gptr is pointing to box2 " << endl;
}
else {
// wont see this though
cout << "gptr is pointing to another box " << endl;
}
cout << "box1 contains the value: " << box1 << endl;
cout << "box2 contains the value: " << box2 << endl;
}
void passByPtr (int *ptr)
{
*ptr = 3;
ptr = gptr;
*ptr = 4;
cout << "______passByPtr has been called________" << endl;
}
void passByPtrRef( int* & ptrRef)
{
*ptrRef = 5;
ptrRef = gptr;
*ptrRef = 6;
cout << "_______passByPtrRef has been called______" << endl;
}
Output:
______________ Current Conditions _____________
p is pointing to box1
gptr is pointing to box2
box1 contains the value: 1
box2 contains the value: 2
Now let’s use PassByPtr
Example
#include <iostream>
using std::cout;
using std::endl;
int box1 = 1;
int box2 = 2;
int * gptr;
void passByPtr(int * ptr);
void passByPtrRef(int* & ptrRef); // Integar pointer reference
int main()
{
int *p = &box1;
gptr = &box2;
passByPtr(p);
cout << "______________ Current Conditions _____________ " << endl;
if (p == &box1){
cout << "p is pointing to box1" << endl;
}
else if (p == &box2 ){
cout << "p is pointing to box2" << endl;
}
else{
cout << "p is pointing to another box " << endl;
}
if (gptr == &box1)
{
cout << "gptr is pointing to box1 " << endl;
}
else if (gptr == &box2)
{
cout << "gptr is pointing to box2 " << endl;
}
else {
// wont see this though
cout << "gptr is pointing to another box " << endl;
}
cout << "box1 contains the value: " << box1 << endl;
cout << "box2 contains the value: " << box2 << endl;
}
void passByPtr (int *ptr)
{
*ptr = 3;
ptr = gptr;
*ptr = 4;
cout << "___passByPtr has been called_____" << endl;
}
void passByPtrRef( int* & ptrRef)
{
*ptrRef = 5;
ptrRef = gptr;
*ptrRef = 6;
cout << "_______passByPtrRef has been called______" << endl;
}
Output:
___passByPtr has been called_____
______________ Current Conditions _____________
p is pointing to box1
gptr is pointing to box2
box1 contains the value: 3
box2 contains the value: 4
Now let’s use PassByRef
#include <iostream>
using std::cout;
using std::endl;
int box1 = 1;
int box2 = 2;
int * gptr;
void passByPtr(int * ptr);
void passByPtrRef(int* & ptrRef); // Integar pointer reference
int main()
{
int *p = &box1;
gptr = &box2;
cout << "______________ Current Conditions _____________ " << endl;
passByPtrRef(p); // Example
if (p == &box1){
cout << "p is pointing to box1" << endl;
}
else if (p == &box2 ){
cout << "p is pointing to box2" << endl;
}
else{
cout << "p is pointing to another box " << endl;
}
if (gptr == &box1)
{
cout << "gptr is pointing to box1 " << endl;
}
else if (gptr == &box2)
{
cout << "gptr is pointing to box2 " << endl;
}
else {
// wont see this though
cout << "gptr is pointing to another box " << endl;
}
cout << "box1 contains the value: " << box1 << endl;
cout << "box2 contains the value: " << box2 << endl;
}
void passByPtr (int *ptr)
{
*ptr = 3;
ptr = gptr;
*ptr = 4;
cout << "___passByPtr has been called_____" << endl;
}
void passByPtrRef( int* & ptrRef)
{
*ptrRef = 5;
ptrRef = gptr;
*ptrRef = 6;
cout << "_______passByPtrRef has been called______" << endl;
}
Output:
_______passByPtrRef has been called______
______________ Current Conditions _____________
p is pointing to box2
gptr is pointing to box2
box1 contains the value: 5
box2 contains the value: 6
Pointer Types, Pointer Arithmetic, Void Pointers
Pointers to Pointers in C/C++
Code
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int x = 5;
int* p = &x;
*p = 6;
int** q = &p;
int*** r = &q;
cout << "*p = " << *p << endl;
cout << "*q = " << *q << endl;
cout << "*(*q) = " << **q << endl;
cout << "*(*r) = " << **r << endl;
cout << "*(*(*r)) = " << ***r << endl;
}
Output:
*p = 6
*q = 0x7ffeee75160c
*(*q) = 6
*(*r) = 0x7ffeee75160c
*(*(*r)) = 6