A Prelude to Pointers
Variables are seen as memory cells that can be accessed using their identifiers. This way we did not have to care about the physical location of our data within memory, we simply used its identifier whenever we wanted to refer to our variable.
The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage . These single-byte memory cells are numbered in a consecutive way, so as, within any block of memory, every cell has the same number as the previous one plus one.
This way, each cell can be easily located in the memory because it has a unique address and all the memory cells follow a successive pattern. For example, if we are looking for cell 1776 we know that it is going to be right between cells 1775 and 1777, exactly one thousand cells after 776 and exactly one thousand cells before cell 2776.
As soon as we declare a variable, the amount of memory needed is assigned for it at a specific location in memory (its memory address). We generally do not actively decide the exact location of the variable within the panel of cells that we have imagined the memory to be
That is a task automatically performed by the operating system during runtime. However, in some cases we may be interested in knowing the address where our variable is being stored during runtime in order to operate with relative positions to it.
The address that locates a variable within memory is what we call a reference to that variable. This reference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of".
t = &a;
This would assign to t the address of variable a, since when preceding the name of the variable a with the reference operator (&) we are no longer talking about the content of the variable itself, but about its reference (i.e., its address in memory).
We have just seen that a variable which stores a reference to another variable is called a pointer. Pointers are said to "point to" the variable whose reference they store.
Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by".
b = *t;
(that we could read as: "b equal to value pointed by t") b would take the value 25, since t is 1776, and the value pointed by 1776 is 25.
Notice the difference between the reference and dereference operators:
• & is the reference operator and can be read as "address of"
• * is the dereference operator and can be read as "value pointed by"
Thus, they have complementary (or opposite) meanings.
A variable referenced with & can be dereferenced with *.
a = 25;
t = &a;
Right after these two statements, all of the following expressions would give true as result:
a == 25
&a == 1776
t == 1776
*t == 25
The first expression is quite clear considering that the assignment operation performed on a was a=25. The second one uses the reference operator (&), which returns the address of variable a, which we assumed it to have a value of 1776. The third one is somewhat obvious since the second expression was true and the assignment operation performed on t was t=&a. The fourth expression uses the reference operator (*) that, as we have just seen, can be read as "value pointed by", and the value pointed by t is indeed 25.
So, after all that, you may also infer that for as long as the address pointed by t remains unchanged the following expression will also be true:
*t == a
Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its declaration which data type a pointer is going point to. It is not the same thing to point to a char than to point to an int or a float.
The declaration of pointers follows this format:
type * name;
where type is the data type of the value that the pointer is intended to point to. This type is not the type of the pointer itself! but the type of the data the pointer points to.
int * number;
char * character;
float * greatnumber;
These are three declarations of pointers. Each one is intended to point to a different data type, but in fact all of them are pointers and all of them will occupy the same amount of space in memory . Nevertheless, the data to which they point to do not occupy the same amount of space nor are of the same type: the first one points to an int, the second one to a char and the last one to a float. Therefore, although these three example variables are all of them pointers which occupy the same size in memory, they are said to have different types: int*, char* and float* respectively, depending on the type they point to.
We now emphasize that the asterisk sign (*) that we use when declaring a pointer only means that it is a pointer (it is part of its type compound specifier), and should not be confused with the dereference operator that we have seen a bit earlier, but which is also written with an asterisk (*). They are simply two different things represented with the same sign.
Now have a look at this code:
First, we have assigned as value of fpointer a reference to fvalue using the reference operator (&). And then we have assigned the value 10 to the memory location pointed by fpointer, that because at this moment is pointing to the memory location of fvalue, this in fact modifies the value of fvalue.
In order to demonstrate that a pointer may take several different values during the same program I have repeated the process with svalue and that same pointer, fpointer.
Here is an example a little bit more elaborated:
Notice that there are expressions with pointers p1 and p2, both with and without dereference operator (*).
The meaning of an expression using the dereference operator (*) is very different from one that does not: When this operator precedes the pointer name, the expression refers to the memory location pointed by it, while when a pointer name appears without this operator, it refers to the value of the pointer itself, that is, a reference.
Another thing that may call your attention is the line:
int * p1, * p2;
This declares the two pointers used in the previous example. But notice that there is an asterisk (*) for each pointer, in order for both to have type int* (pointer to int).
Otherwise, the type for the second variable declared in that line would have been int (and not int*) because of precedence relationships.
If we had written:
int * p1, p2;
p1 would indeed have int* type, but p2 would have type int (spaces do not matter at all for this purpose). This is due to operator precedence rules. But anyway, simply remembering that you have to put one asterisk per pointer is enough for most pointer users.
Re: A Prelude to Pointers
|All times are GMT +5.5. The time now is 19:39.|