How To Use Pointers In C Plus Plus (CPP) (C++)

Language
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;
  1. #include "stdafx.h";
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. void editor(int test) {
  8.         test = 100;
  9.         cout << "Editor function's local scope: " << test << endl;
  10. }
  11.  
  12. int main(int numberOfargs, char* args[]) {
  13.         int var = 1;
  14.         cout << var << endl;
  15.         editor(var);
  16.         cout << var << endl;
  17.         system("PAUSE");
  18.         return 0;
  19. }
The output of the program is:
  1. 1
  2. Editor function's local scope: 100
  3. 1
  4. 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...
  1. 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...
  1. void editor(int* test) {
Finally, we are receiving one last error. On the line...
  1. 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'...
  1. *test = 100;
If we run the program now, you can see that the value change is now being saved successfully/globally!
  1. 1
  2. Editor function's local scope: 0086FB0C
  3. 100
  4. 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...
  1. cout << "Editor function's local scope: " << *test << endl;
Finished! Here is the full finished source...
  1. #include "stdafx.h";
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. void editor(int* test) {
  8.         *test = 100;
  9.         cout << "Editor function's local scope: " << *test << endl;
  10. }
  11.  
  12. int main(int numberOfargs, char* args[]) {
  13.         int var = 1;
  14.         cout << var << endl;
  15.         editor(&var);
  16.         cout << var << endl;
  17.         system("PAUSE");
  18.         return 0;
  19. }

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.

Add new comment