Created
March 26, 2012 02:37
-
-
Save tomthorogood/2202534 to your computer and use it in GitHub Desktop.
An exploration of pointers in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // I've been trying to better understand pointers, so I made this as a reference for myself. | |
| // Figured it wouldn't hurt to make it available if anyone else is having any problems. | |
| #include <iostream> | |
| using namespace std; | |
| int main() | |
| { | |
| // Let's declare a standard integer variable. | |
| int orig = 1013; | |
| // Now, we'll create a copy of this variable. | |
| int copy = orig; | |
| // If instead of creating more copies of this variable, | |
| // we can instead pass around the address of this | |
| // variable's location in memory. | |
| // We do this by declaring a pointer variable. | |
| // | |
| // The following statement says: | |
| // we are pointing to a location in memory where an int | |
| // is stored. | |
| // | |
| // For comparison, we've created two references. | |
| // the first points to our original integer, | |
| // and the second to our copy. You can see that | |
| // these two are clearly stored in different memory locations. | |
| // | |
| int * andRefO = &orig; | |
| int * andRefC = © | |
| // Lastly, once we're ready to do something with the | |
| // integer itself, we can derefence it. The following | |
| // statement effectively says: | |
| // go to this location and memory and use the int that's stored there. | |
| int deref = *andRefO; | |
| int * andRefD = &deref; | |
| cout << "orig : " << orig << " \t\t\t\t | The original integer variable " << endl; | |
| cout << "copy : " << copy << " \t\t\t\t | The copy of the integer variable " << endl; | |
| cout << "andRefO : " << andRefO << " \t\t | The location in memory where the original variable is stored " << endl; | |
| cout << "andRefC : " << andRefC << " \t\t | The location in memory where the copy of the variable is stored " << endl; | |
| cout << "deref : " << deref << " \t\t\t\t | Just for completion, we've derefenced the location in memory where the first variable is stored " << endl; | |
| cout << "andRefD : " << andRefD << "\t\t | And its location in memory. " << endl; | |
| // Now, we can change the value of original variable via its reference: | |
| // These next statements effectively say: | |
| // Change the value of the integer stored in the memory location pointed to by this variable. | |
| *andRefO = 1121; | |
| *andRefC = 4815; | |
| cout << "orig : " << orig << " \t\t\t\t | The original variable having been changed by way of its reference. " << endl; | |
| cout << "copy : " << copy << " \t\t\t\t | The copy of the original variable, having been changed by way of its own reference. " << endl; | |
| cout << "andRefO : " << andRefO << " \t\t | The location in memory where the original variable is stored " << endl; | |
| cout << "andRefC : " << andRefC << " \t\t | The location in memory where the copy of the variable is stored " << endl; | |
| cout << "deref : " << deref << " \t\t\t\t | For completion, the dereferenced pointer of the original variable's reference. " << endl; | |
| cout << "andRefD : " << andRefD << "\t\t | And its location in memory. " << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment