VARIABLES VS. POINTERS
In any programming language Variables are declared, and a main memory (RAM) “Pocket” is reserved for each of them
at a location defined by what is known as their “Address”. Is in this corresponding “Pocket” in memory for each
Variable that its value is stored. This is because the computer has to be able to differentiate between values
belonging to Variables and “Pockets” in main memory serve exactly this purpose. This way the existence of a
Variable is determined by two things and two things only: a) its main memory “Address”, and b) its Value. In most
of the so called “High-level” programming languages, we are allowed to access directly only the value of a Variable.
In C/C++, however, we are able to access both the value and the main memory “Address” defining the main memory
“Pocket” of a Variable. This additional degree of freedom is provided to us through the concept of “Pointers” with
the assistance of a couple of special operators. In this respect, to access the value of a Variable suffices to make
a direct reference to the name of the Variable, as in the example below:
In this example, we declared a Variable by the name “X” its Data-type being “integer” and initialised it by
assigning to it the value “10”. This way we fulfil the two requirements (main memory “Address”, name “X” and
Value “10”) and the Variable successfully starts its life in our program. The only command existing in the
example above prints the Variable’s value on the screen by making a direct reference to its name as an
“integer” Data-type Variable. On the other hand, we may want to access the Variable’s main memory “Address”
instead, as in the example below:
The only command existing in this example is printing on the screen the “Address” of the Variable in main memory
(if we “Run” the program again and again we will make a very interesting observation; we take a different main
memory “Address” for the Variable with every time we “Run” the program). The medium through which we are now able
to refer and retrieve the main memory “Address” of the Variable instead of its value is the “&” operator. This is
the so called “Address-of” operator.
In this respect, we can safely consider the Name of a Variable as representing in a meaningful way to us the
main memory “Pocket” its value lies into (a human mnemonic).
Let us explain a bit further this statement. When we want to Declare a Variable in C/C++, we do it by initiating
a Declaration Command similar to the following:
In reality we do two things by initiating this declaration statement: a) we reserve a “Pocket” in main memory, that
is, now we have at our disposal a specific location in main memory, accessible through an “Address”, in which to
store potential values, and b) by initialising the Variable to “10” we store in this “Pocket” that became available
to us the value “10”. From this becomes apparent that the actual name of the Variable (in this case “X”) serves the
only purpose of providing us with an easy mnemonic through which we can easily refer to an actual “Address” in main
memory. That “Address” characterises a main memory “Pocket” capable of holding a value of the specific Data-type the
Variable has been assigned (in this case “int”) in its declaration command. Is exactly this characteristic that makes
possible the existence and the use of Pointers in the first place. We declare a Pointer and initialise it to a specific
Variable, as shown below:
The first thing we need to notice is that to declare a Pointer we do it through using the “*” operator, the so called
“Indirection” operator. Again, as in the previous example, we first reserve a “Pocket” in main memory for our Pointer
(because a Point starts its life as a Variable), and then by initialising the Pointer to the “Address” of Variable “X”
we store this “Address” as a value in the particular Pocket in main memory (is exactly at this point that the Pointer
gains its Pointer status). In this case the human mnemonic for the “Pocket” in main memory holding as a value the main
memory “Address” of Variable “X” is “PointerToVariable” (under the light of what discussed above a more appropriate and
descriptive name would have been: “PointerToAMainMemoryPocketAddress”, but this name is too long and very inconvenient
to use in a program).
So, in a Pointer we never have an actual value by definition, what we have is always a main memory “Address”. In our
case we do not have “X” in Pointer “PointerToVariable”, we have the main memory “Address” of “X”. The truly remarkable
thing with this is that we can now use and manipulate the value in “X” without actually referring to “X”.