Created
March 11, 2013 18:39
-
-
Save ereidland/5136514 to your computer and use it in GitHub Desktop.
Example 1 at Library
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
#include <iostream> | |
using namespace std; | |
void refFunc(int &);//Function headers. They do not require a name or body at this point. | |
void pointerFunc(int *); | |
int main() | |
{ | |
int something = 32; //Declare something as 32. | |
cout << something << endl; // Print it. | |
refFunc(something); //Pass "something" as reference variable. This is handled by the function arguments. | |
cout << something << endl; // Print it. | |
pointerFunc(&something); //The & passes the address of the variable instead of just directly passing the variable. | |
cout << something << endl; // Print it. | |
return 0; //Result to be sent back to the OS once this is complete. | |
} | |
void refFunc(int & var) //Note that these much directly match the arguments (at least by type) in the function headers. | |
{ | |
var = 64; //No special operators required to set the value. | |
} | |
void pointerFunc(int * var) | |
{ | |
*var = 128; //The * before the variale on this line de-references it. | |
//The argument is the address, and the de-referencing gets the value at the address. | |
} | |
//Functionally, reference variables and pointers do the same thing, but reference variables can be easier to read. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment