Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1K views

C++ Pointer Practice

Pointers allow a variable to store the memory address of another variable. The & operator returns the memory address of a variable and * accesses the value at a specific memory address. In the code, two variables firstvalue and secondvalue are declared and initialized. Two pointers p1 and p2 are declared and initialized to NULL. p1 is assigned the memory address of firstvalue using &. p2 is assigned the memory address of secondvalue. *p1 and *p2 are used to access and modify the values of firstvalue and secondvalue. The pointers are reassigned and used to swap the values.

Uploaded by

martin napanga
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

C++ Pointer Practice

Pointers allow a variable to store the memory address of another variable. The & operator returns the memory address of a variable and * accesses the value at a specific memory address. In the code, two variables firstvalue and secondvalue are declared and initialized. Two pointers p1 and p2 are declared and initialized to NULL. p1 is assigned the memory address of firstvalue using &. p2 is assigned the memory address of secondvalue. *p1 and *p2 are used to access and modify the values of firstvalue and secondvalue. The pointers are reassigned and used to swap the values.

Uploaded by

martin napanga
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Pointers

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;

p1 = &firstvalue; // p1 = address of firstvalue


p2 = &secondvalue; // p2 = address of secondvalue

*p1 = 10; // value pointed to by p1 = 10


*p2 = *p1; // value pointed to by p2 = value pointed to by p1

p1 = p2; // p1 = p2 (value of pointer is copied)

*p1 = 20; // value pointed to by p1 = 20

cout << "firstvalue is " << firstvalue << '\n';


cout << "secondvalue is " << secondvalue << '\n';
return 0;
}
// http://www.cplusplus.com/doc/tutorial/pointers/

https://www.codeproject.com/Articles/11560/Pointers-Usage-in-C-Beginners-to-Advanced
http://www.cplusplus.com/doc/tutorial/pointers/ 1
Pointers
pointer diagram

variable or pointer address

value of variable or pointer

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

p1 &p1 = 008FFA78 p2 &p2 = 008FFA6C

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

p1 &p1 = 008FFA78 p2 &p2 = 008FFA6C

008FFA90 008FFA84

firstvalue = 5
&firstvalue = 008FFA90

secondvalue = 15
&secondvalue = 008FFA84

p1 = 008FFA90 (p1 = &firstvalue)


after above statements: *p1 = 5 (the value pointed to by p1 = 5)
&p1 = 008FFA78 (address of an address value)
&*p1 = 008FFA90

p2 = 008FFA84 (p2 = &secondvalue)


*p2 = 15 (the value pointed to by p2 = 15)
&p2 = 008FFA6C (address of an address value)
&*p2 = 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

p1 &p1 = 008FFA78 p2 &p2 = 008FFA6C

008FFA90 008FFA84

firstvalue = 10
&firstvalue = 008FFA90

secondvalue = 10
&secondvalue = 008FFA84

p1 = 008FFA90 (p1 = &firstvalue)


after above statements: *p1 = 10 (the value pointed to by p1 = 10)
&p1 = 008FFA78 (address of an address value)
&*p1 = 008FFA90

p2 = 008FFA84 (p2 = &secondvalue)


*p2 = 10 (the value pointed to by p2 = 10)
&p2 = 008FFA6C (address of an address value)
&*p2 = 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

p1 &p1 = 008FFA78 p2 &p2 = 008FFA6C

008FFA84 008FFA84

firstvalue = 10
&firstvalue = 008FFA90

secondvalue = 10
&secondvalue = 008FFA84

p1 = 008FFA84 (p1 = &secondvalue)


after above statements: *p1 = 10 (the value pointed to by p1 = 10)
&p1 = 008FFA78 (address of an address value)
&*p1 = 008FFA84

p2 = 008FFA84 (p2 = &secondvalue)


*p2 = 10 (the value pointed to by p2 = 10)
&p2 = 008FFA6C (address of an address value)
&*p2 = 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

p1 &p1 = 008FFA78 p2 &p2 = 008FFA6C

008FFA84 008FFA84

firstvalue = 10
&firstvalue = 008FFA90

secondvalue = 20
&secondvalue = 008FFA84

p1 = 008FFA84 (p1 = &secondvalue)


after above statements: *p1 = 20 (the value pointed to by p1 = 20)
&p1 = 008FFA78 (address of an address value)
&*p1 = 008FFA84

p2 = 008FFA84 (p2 = &secondvalue)


*p2 = 20 (the value pointed to by p2 = 20)
&p2 = 008FFA6C (address of an address value)
&*p2 = 008FFA84

https://www.codeproject.com/Articles/11560/Pointers-Usage-in-C-Beginners-to-Advanced
http://www.cplusplus.com/doc/tutorial/pointers/ 6

You might also like