Skip to content

Instantly share code, notes, and snippets.

@rjchatfield
Created March 4, 2014 12:00
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 rjchatfield/9345223 to your computer and use it in GitHub Desktop.
Save rjchatfield/9345223 to your computer and use it in GitHub Desktop.
Who needs help with references and dereferencing? I just made this tiny program to help me understand what `int *myInt` meant at different places.
#include <iostream>
using namespace std;
void myFunc(int *r) //-- If you dereference this `r`, you'll get an int
{
cout << " r: " << r << endl; //-- `r` is just a reference
cout << " *r: " << *r << endl; //-- Dereference a reference, gives me an int
}
int main(int argc, const char * argv[])
{
int myInt = 69;
cout << " myInt: " << myInt << endl; //-- `myInt` is an int
cout << "&myInt: " << &myInt << endl; //-- `&myInt` is an reference
myFunc(&myInt); //- & is a reference
return 0;
}
/*
myInt: 69
&myInt: 0x7fff5fbff8fc
r: 0x7fff5fbff8fc
*r: 69
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment