Variable, Reference To Variable, Pointer To Variable

A variable is declared as follows:

int x;


X is the variable name

int specified the type of data that can be stored in x.

Let us assume that the variable shown above is declared in the void main of the program. The variable scope is inside the main and we know that the variable occupies some memory in the call stack of main. Now the x is a naming given to the location in the stack that is allocated by the void main function. Using the assignment operator and the variable x the location is accessed. So the address involved is hided behind the scene and all you know is variable name x and internally is has association to location in the stack memory of the main function.

Now have a look at the variable declaration below:

int& refto_x = x; 


refto_x is reference variable and that references the variable x. Int& specifies the reference notation and reference to what data type.

The reference variable should be initialized when it is declared. See… in our example statement we declared a reference variable refto_x and initialized it with the assignment = x. What does that say? Well. X is a naming notation used internally to read or write from a specific memory location in a stack (When x is not a global variable and declared inside some function) and simply it is a variable declared already. And refto_x is a naming notation for the naming x. That means refto_x is simply one more naming given to the location represented by the variable x.

Now look at the statement below,

int * pointerto_x = &x;


int * - Represents pointer to an integer

pointerto_x – A variable name given to the pointer variable

&x – Address of the variable x.

In the above declaration a pointer variable pointerto_x is declared and at the same time initialized to have the address of the variable x. Note that in the above statement unlike reference it is not just one more name to the location represented by x. It has it own location represented by a variable pointerto_x and that location can store address location and when it access that location it treats that address location to read or write integer sized data.