C++ Pointer Practice
C++ Pointer Practice
A pointer is a variable whose value is the address of another variable. Like any variable or constant, you
must declare a pointer before you can work with it. The & operator gives us the address of a variable
and * gives us the value of a variable at a specified address. For example:
// more pointers
#include <iostream>
using namespace std;
int main ()
{
int firstvalue = 5, secondvalue = 15;
int *p1 = NULL;
int *p2 = NULL;
https://www.codeproject.com/Articles/11560/Pointers-Usage-in-C-Beginners-to-Advanced
http://www.cplusplus.com/doc/tutorial/pointers/ 1
Pointers
pointer diagram
1st step
program declarations
int firstvalue = 5, secondvalue = 15;
int *p1, *p2
explanation:
1. firstvalue = 5
2. secondvalue = 15
3. *p1 = null
4. *p2 = null
variables
&firstvalue = &secondvalue =
firstvalue secondvalue
008FFA90 008FFA84
5 15
pointers
0 0
https://www.codeproject.com/Articles/11560/Pointers-Usage-in-C-Beginners-to-Advanced
http://www.cplusplus.com/doc/tutorial/pointers/ 2
Pointers
2nd step
program statements
p1 = &firstvalue
p2 = &secondvalue
explanation:
1. p1 value equals firstvalue address (008FFA90)
2. p2 value equals secondvalue address (008FFA84)
variables
&firstvalue = &secondvalue =
firstvalue secondvalue
008FFA90 008FFA84
5 15
pointers
008FFA90 008FFA84
firstvalue = 5
&firstvalue = 008FFA90
secondvalue = 15
&secondvalue = 008FFA84
https://www.codeproject.com/Articles/11560/Pointers-Usage-in-C-Beginners-to-Advanced
http://www.cplusplus.com/doc/tutorial/pointers/ 3
Pointers
3rd step
program statements
*p1 = 10
*p2 = *p1
explanation:
1. the value pointed to by p1 = 10;
p1 = &firstvalue = 008FFA90;
change value at 008FFA90 to 10
2. the value pointed to by p2 = the valued pointed to by p1 = 10;
p2 = 008FFA84
change value at 008FFA84 to 10
variables
&firstvalue = &secondvalue =
firstvalue secondvalue
008FFA90 008FFA84
10 10
pointers
008FFA90 008FFA84
firstvalue = 10
&firstvalue = 008FFA90
secondvalue = 10
&secondvalue = 008FFA84
https://www.codeproject.com/Articles/11560/Pointers-Usage-in-C-Beginners-to-Advanced
http://www.cplusplus.com/doc/tutorial/pointers/ 4
Pointers
4th step
program statements
p1 = p2
explanation:
1. value of p1 = value of p2 (both p1 and p2 are address values)
2. p1 = p2 = 008FFA84 = &secondvalue
3. now, p1 will not point to &firstvalue
variables
&firstvalue = &secondvalue =
firstvalue secondvalue
008FFA90 008FFA84
10 10
pointers
008FFA84 008FFA84
firstvalue = 10
&firstvalue = 008FFA90
secondvalue = 10
&secondvalue = 008FFA84
https://www.codeproject.com/Articles/11560/Pointers-Usage-in-C-Beginners-to-Advanced
http://www.cplusplus.com/doc/tutorial/pointers/ 5
Pointers
5th step
program statements
*p1 = 20
explanation:
1. the value pointed to by p1 = 20;
2. p1 = &seccondvalue = 008FFA84;
3. change variable value at 008FFA84 to 20;
variables
&firstvalue = &secondvalue =
firstvalue secondvalue
008FFA90 008FFA84
10 20
pointers
008FFA84 008FFA84
firstvalue = 10
&firstvalue = 008FFA90
secondvalue = 20
&secondvalue = 008FFA84
https://www.codeproject.com/Articles/11560/Pointers-Usage-in-C-Beginners-to-Advanced
http://www.cplusplus.com/doc/tutorial/pointers/ 6