Before learning pointers, one needs to have a basic understanding of some essential concepts. Pointers are one of the most confusing topics, and to understand it better, this tutorial shall be considered as a prerequisite to get started with pointers in no time. So, let’s dive in and get ourselves ready for the journey ahead.
The First Step–Understanding Variables
Most beginners find it difficult to understand pointers because they usually have minimal understanding of variables. Variables and pointers are quite closely related. According to me, understanding variables should be the prerequisite to understand variables. So, let’s properly understand the concept of variables first.
A variable is a name given to a memory location which allows us to access that location for storage. In most modern computers, memory is divided into multiple blocks where the size of each block is 1 byte. So, n bytes of memory have n blocks. This is how you can imagine a memory in a computer:
When a variable is declared by the programmer, it informs two things to the compiler–the data type and the name. Recall how to declare a variable in C:
int var;
The above variable has `int` data type and the name of the variable is `var`. According to the data type, the compiler allocates memory to this variable (typically on most modern computers, an integer variable takes 4 bytes of memory) and not only this, the compiler also creates a symbol table to associate the name of the variable with the memory address. Also, it is worth noting that it is always the address of the first byte of memory which is allocated to a variable. In our example, if the variable var is assigned 4 bytes of memory, then the address of the variable is the same as the address of the first byte of memory. The following image depicts the same:
Does that mean we can access only the first byte of memory? No. Knowing the address of the first byte of memory gives access to the remaining bytes because the compiler always allocates contiguous blocks of memory (memory blocks that are closely associated). Therefore, from the first block’s address one can easily calculate the address of the remaining blocks.
Now, we know what happens when a variable is created. But, what happens when we write the following line of code?
var = 10;
We are assigning value 10 to the variable by using the assignment operator (=). The job of the assignment operator is responsible to assign what we call the rvalue (value 10) to the lvalue (variable var).
The meaning of lvalue and rvalue should be crystal clear. The rvalue represents the simple value which can be written as is (like 10) or which can be obtained as the result of some expression evaluation or a function call. And, lvalue represents some memory location where we can store some value. In the left hand side of the assignment operator, we are not allowed to use an rvalue. So the following line of code is illegal:
10 = var;
So, we have two types of values:
Simple values: like 10, ‘a’, 3.14159.
Memory addresses
One can easily find the memory address associated with a variable by using the & (the address of) operator like the following:
#include <stdio.h>
int main() {
int var = 10;
printf("%p", &var);
return 0;
}
Output:
0x7fc340da98
The %p format specifier can be used to display memory address associated with a variable in the hexadecimal format as shown in the output.
Now, the most important question–can we store the memory address, just like a normal value in a variable? The answer is we can store memory addresses but they shouldn’t be treated as normal values. Their memory requirements are different (Typically, a memory address requires 8 bytes of memory) and to prevent accidental misuse of memory, the distinction between a normal value and a memory address is important. Thankfully, in the C programming language, we have special types of variables which are designed to hold memory addresses, and these variables are called Pointers.
Pointers as Variables
As mentioned, a pointer is a variable which is capable of storing a memory address. So, we can say, it points to a location in memory (that’s why the name Pointer 😉). I know at this moment, there are a couple of questions in your mind like–how to create pointers and assign addresses to them? What pointers are good for? Why do we store addresses? Don’t worry! The answers to these questions will be given in the coming tutorials in detail.
Leave a comment