Last active
August 29, 2015 13:59
-
-
Save axrwkr/10550866 to your computer and use it in GitHub Desktop.
C++ Reference and Dereference Operators
This file contains 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
/* | |
on os x compile with (install xcode and command line tools) | |
clang++ pointers.cpp -o pointers | |
on windows compile with (install visual studio) | |
cl /EHsc pointers.cpp | |
on linux compile with (apt-get install g++) | |
g++ pointers.cpp -o pointers | |
*/ | |
#include <iostream> | |
int main() | |
{ | |
// assign a value to the variable | |
int variable = 75; | |
// get the address of the variable (a reference/pointer to the variable) | |
int * pointer = &variable; | |
// display the value of the variable | |
std::cout << "The variable value is " << variable << std::endl; | |
// display the address of the variable | |
std::cout << "The address of the variable is " << pointer << std::endl; | |
// change the value of the variable using the pointer | |
*pointer = 45; | |
std::cout << "Changing the value of the variable using a refence to its address" << std::endl; | |
// display the value of the variable | |
std::cout << "The value of the variable is " << variable << std::endl; | |
// display the value of the dereferenced variable | |
std::cout << "The value via the pointer is " << *pointer << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment