However, not every binary number is created equal...
But there are also binary numbers in the computer are used to represent addresses of variables (these variables can be simple variables - like an integer - or very complex - we call complex variables "structures" and even "objects").
 
The reference Operator & operates on a (single) variable name and returns the address of that variable   |
int i1, i2; short s1, s2; char c1, c2; float f1, f2; double d1, d2; int main(int argc, char *argv[]) { cout << "Address of i1 = " << (unsigned int) &i1 << "\n"; cout << "Address of i2 = " << (unsigned int) &i2 << "\n"; cout << "Address of s1 = " << (unsigned int) &s1 << "\n"; cout << "Address of s2 = " << (unsigned int) &s2 << "\n"; cout << "Address of c1 = " << (unsigned int) &c1 << "\n"; cout << "Address of c2 = " << (unsigned int) &c2 << "\n"; cout << "Address of f1 = " << (unsigned int) &f1 << "\n"; cout << "Address of f2 = " << (unsigned int) &f2 << "\n"; cout << "Address of d1 = " << (unsigned int) &d1 << "\n"; cout << "Address of d2 = " << (unsigned int) &d2 << "\n"; } |
 
The dereference Operator * operates on an address (an unsigned integer !) and returns the value stored at that address   |
We have no idea what type of value is stored at that address !!!
The way to do that is:
See examples below.
int i1 = 1234, i2; short s1 = -54, s2; char c1, c2; float f1 = 3.14, f2; double d1 =2.718, d2; int main(int argc, char *argv[]) { cout << (unsigned int) &i1 << "\n"; cout << "int value at 136760 = " << * (int*)136760 << "\n"; cout << (unsigned int) &f1 << "\n"; cout << "float value at 136764 = " << * (float*)136764 << "\n"; cout << (unsigned int) &d1 << "\n"; cout << "double value at 136768 = " << * (double*)136768 << "\n"; } |
  Just like programs need to define int type variables to help programmers store and manipulate integer values, the programming language allows the programmer to define special type of variables to store address values   |
Hence, when these special type of "address storing" variables are define, we must also specify what type of data is stored at that location.
Hence, a veriable that contains an address is called a reference variable
If we mentally draws a line from the location of the reference variable to the address contained in that variable, we have a line "pointing" to the referenced variable:
TYPE * variableName; |
int * p; // variable p holds an address of an int variable double * q; // variable q holds an address of a double variable |
int a; int * p; // variable p holds an address of an int variable p = &a; // &a = the address of the integer variable a // So: assign the address of the integer variable a to p // p now contains the address of the integer variable a // We say: p "points to" a *p = 1234; // Put the value 1234 into the (integer) variable // pointed to by p // The variable a will be changed !!! |
In other words: you should make (int *) type pointer variable point to integer valued variables. And make (double *) type pointer variable point to double valued variables, etc, etc.
In C++, it is illegal to do this:
double a; int* p; // p references an integer values variable p = &a; // make p point to a double valued variable.... // ILLEGAL !!! |
Because the address is passed to the function, whenever statements inside the function need to use the value of the variable, it must first get the value through the address value.
The dereference operator * can be used to obtain the value stored at any given address
int main(int argc, char *argv[]) { int x, y; void f(int*, int*); // pass by value ! x = 10; y = 100; cout << "Before f(x,y), x = " << x << ", y = " << y << "\n"; f(&x, &y); // pass by "address of x and y" by value ! cout << "After f(x,y), x = " << x << ", y = " << y << "\n"; } void f(int * a, int * b) { cout << "Before increment a = " << a << ", b = " << b << "\n"; *a = *a + 10000; *b = *b + 10000; cout << "After increment a = " << a << ", b = " << b << "\n"; } |
  C does not have the "Pass-by-reference" mechanism and programmers have to use the above scheme to pass parameter by reference "explicitly"   |