Pointers are subject of fear for a large number of people. Some of them hate pointers because they can cause nasty problems that are difficult to debug, others hate them because they never really understood what pointers actually are. Indeed, a person who had never learned assembly language will find it hard to understand the nature and the consequences of pointers. Therefore, let’s start at the very beginning.
As you already know, all variables must be declared prior to their first usage in many programming languages.
A declaration will cause the system to allocate 32 bits somewhere in your computer’s memory. This is where a value (number) you assign to this variable will go. In many cases a programmer might not be interested in where exactly this place is. In fact, in your PC a variable might be ending up in different places each time you are running your program (*).
In many situations the programmer would not be interested on which memory address the value of his variable is ending up. There are some situations, however, where you can do fancy stuff by working with addresses rather than values.
Consider, for example, that you want to pass a large amount of data (e.g. stored in an array) to a function or method. If you would pass this data by value, this would require a large amount of stack memory to be used for local storage of the data. The reason is that all parameters of a function or method are being stored in the same way local variables are being stored: On a local variable stack. In most systems the stack size is (very) limited, because large stacks would negatively influence the runtime of function invocations.
So it might be much wiser to pass the address where in memory the data can be found instead of passing the whole bunch of data. In case of arrays three pieces of information would be sufficient to be known inside the function:
- the address of the first array element
- the size of each element (determined by the data type)
- the length of the array (number of elements or end)
Working with addresses requires the compiler to handle a special data type: The pointer, sometimes referred to as reference.
Some people say that a pointer is not a variable, which is not exactly true. A pointer is a variable, but its value is neither a phone number nor a text message. Instead, the value of the pointer variable is the address in your computer’s memory under which the phone number or text message can be found. As we say, a pointer variable is pointing to a value stored somewhere else in memory.
Some programming languages (such as C) allow declaration of pointer variables for all kinds of data types. Some languages (such as Java) do not allow declaration of pointers for primitive data types, but use pointers for complex data types (e.g. arrays, objects, …). Other languages do not support any pointers at all (BASIC).
Let’s have a look at C to get an impression of how pointers are handled.
A pointer only makes sense when there is an existing value that can be pointed to. The following code snippet shows the declaration of a variable a and a pointer variable named pointer_var. The asterisk before the variable name indicates that this variable is a pointer.
int a; int *pointer_var;
pointer_var is now declared to hold addresses under which int values are being stored.
If you now want pointer_var to contain the address of variable a you just have to add the following line of code.
pointer_var = &a;
The ampersand symbolizes the words “address of“.
Now there are two ways of assigning values to variable a.
1. Directly (by value), which is the way you know and you probably find the most convenient.
a = 5;
2. Indirectly (by reference), which implies using the address of a stored in the pointer variable.
*pointer_var = 5;
What make the syntax difficult to understand for some people is the different meaning of the asterisk. Here it is used for “dereferencing” the pointer. You can read *pointer_var like this : “Value that is stored at the address which is given by pointer_var“.
That’s all for a brief introduction of what pointers actually are. A more advanced topic on pointers will follow.
— Andre M. Maier
(*) This is different in computers that do not use any operating system, such as microcontrollers.