TheLegend27
Quantum Network Specialist
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
Introduction:
This tutorial is on how to use Pointers in C++.
Theory:
So why exactly do we need to use pointers? And what are they?
If you write a simple program with two functions and a variable where one of the functions parses the variable to the second function in order for the variables value to change, it will only change in the local scope of the second function. Here is an example;
The output of the program is:
As you can see, the variable's value has only changed in the local scope of the editor function. This is because when the function is called, it only parses a copy of the variable being sent, and so only that copied variable is edited.
So, if we use a pointer for this variable, we are able to bridge the gap, remove the need for a copy of the variable, and therefore edit only one version of the globally available (in essence) variable.
Pointer Elements:
The way pointers work, is instead of sending a copy of a variable to function calls and other locations, they just send the memory address of where the variable(s) are held within the computers RAM (Random Access Memory/Temporary Memory). These locations are all in hexadecimal values. The function can then use that address location to edit the variable directly so once the variable is then accessed from the main function call location, the value is already edited and saved.
There are two pointer 'elements';
& - Retrieves the location/value of a variable
* - Holds the location/value of a variable
Program Modifications:
There are only two changes necessary to implement pointers successfully in to our test program above. First we need to send only the memory address location of the integer variable to our 'editor'function, as opposed to a copy of the local variable...
Now that we are sending our location only, we are getting an error in our 'editor' function because it is expecting an integer variable, not an integer memory address location. We can edit this by changing our 'int' parameter on our 'editor' function, to 'int*'. This holds an integer memory address location for the parsed argument/parameter...
Finally, we are receiving one last error. On the line...
This is because we simply don't have a 'test' variable anymore. Instead it wants to read the variable as a location, so we use '*test'...
If we run the program now, you can see that the value change is now being saved successfully/globally!
There's just one problem, we are receiving the memory address location of our test variable on our second output line. That is simply changed by again adding an asterix (*) in front of our 'test' variable reference on our 'cout' line...
Finished!
Here is the full finished source...
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
This tutorial is on how to use Pointers in C++.
Theory:
So why exactly do we need to use pointers? And what are they?
If you write a simple program with two functions and a variable where one of the functions parses the variable to the second function in order for the variables value to change, it will only change in the local scope of the second function. Here is an example;
- #include "stdafx.h";
- #include <cstdio>
- #include <cstdlib>
- #include <iostream>
- using
namespace
std;
- void
editor(
int
test)
{
- test =
100
;
- cout
<<
"Editor function's local scope: "
<<
test <<
endl;
- }
- int
main(
int
numberOfargs, char
*
args[
]
)
{
- int
var =
1
;
- cout
<<
var <<
endl;
- editor(
var)
;
- cout
<<
var <<
endl;
- system
(
"PAUSE"
)
;
- return
0
;
- }
The output of the program is:
- 1
- Editor function's local scope: 100
- 1
- Press any key to continue . . .
As you can see, the variable's value has only changed in the local scope of the editor function. This is because when the function is called, it only parses a copy of the variable being sent, and so only that copied variable is edited.
So, if we use a pointer for this variable, we are able to bridge the gap, remove the need for a copy of the variable, and therefore edit only one version of the globally available (in essence) variable.
Pointer Elements:
The way pointers work, is instead of sending a copy of a variable to function calls and other locations, they just send the memory address of where the variable(s) are held within the computers RAM (Random Access Memory/Temporary Memory). These locations are all in hexadecimal values. The function can then use that address location to edit the variable directly so once the variable is then accessed from the main function call location, the value is already edited and saved.
There are two pointer 'elements';
& - Retrieves the location/value of a variable
* - Holds the location/value of a variable
Program Modifications:
There are only two changes necessary to implement pointers successfully in to our test program above. First we need to send only the memory address location of the integer variable to our 'editor'function, as opposed to a copy of the local variable...
- editor(
&
var)
;
Now that we are sending our location only, we are getting an error in our 'editor' function because it is expecting an integer variable, not an integer memory address location. We can edit this by changing our 'int' parameter on our 'editor' function, to 'int*'. This holds an integer memory address location for the parsed argument/parameter...
- void
editor(
int
*
test)
{
Finally, we are receiving one last error. On the line...
- test =
100
;
This is because we simply don't have a 'test' variable anymore. Instead it wants to read the variable as a location, so we use '*test'...
- *
test =
100
;
If we run the program now, you can see that the value change is now being saved successfully/globally!
- 1
- Editor function's local scope: 0086FB0C
- 100
- Press any key to continue . . .
There's just one problem, we are receiving the memory address location of our test variable on our second output line. That is simply changed by again adding an asterix (*) in front of our 'test' variable reference on our 'cout' line...
- cout
<<
"Editor function's local scope: "
<<
*
test <<
endl;
Finished!
Here is the full finished source...
- #include "stdafx.h";
- #include <cstdio>
- #include <cstdlib>
- #include <iostream>
- using
namespace
std;
- void
editor(
int
*
test)
{
- *
test =
100
;
- cout
<<
"Editor function's local scope: "
<<
*
test <<
endl;
- }
- int
main(
int
numberOfargs, char
*
args[
]
)
{
- int
var =
1
;
- cout
<<
var <<
endl;
- editor(
&
var)
;
- cout
<<
var <<
endl;
- system
(
"PAUSE"
)
;
- return
0
;
- }
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
You must upgrade your account or reply in the thread to view the hidden content.