Skip to content

Instantly share code, notes, and snippets.

@axrwkr
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axrwkr/10550866 to your computer and use it in GitHub Desktop.
Save axrwkr/10550866 to your computer and use it in GitHub Desktop.
C++ Reference and Dereference Operators
/*
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