Do you know what the below line of code means?
If the answer is yes then learning pointers will be fairly easy for you. Just make sure you type all code samples provided and practice whatever is being asked to do.
The pointer is just another data type in C/C++. Just like how an integer (the 4-byte ones) is used to store numbers (positive and negative) similarly pointer is a data type that allows storing addresses.
How come addresses?
When you run your programs, each program runs under a process and each process is given some memory by the system. All the data(variables values etc) get stored in this memory dedicated to your program. This memory given by the system resides in RAM and each byte of RAM has a unique identifier called as its address.
Now pointers in C allow us to access those memory addresses directly and manipulate them as well. Consider this code.
The line printf (“%d”, i) use the format specifier %d to print integers. In the next line, you see a %p. This is used to print addresses or pointer types in C. The &I uses the address-of operator i.e. & to get the memory address of the variable i.
Running above code on my system gives this output on my system:
This output is an address in hexadecimal notation. It will vary on your system and will mostly vary each time you run this code. Give it a try!
Additionally try to print memory addresses of other types of variables: floats, chars, etc. And remember the %p format specifier and the & (address-of) operator are mandatory while working with pointers.
Storing addresses and Pointers:
Now you know that the & symbol gives you the address of a variable. So now the question is how to store this new kind of data which we call as an address of some variable. The obvious answer is pointers.
Just imagine a pointer as a variable that can store addresses just like how your int variables can store positive and negative numbers and your float variable can store positive and negative real numbers etc.
For pointer creation, C uses the * operator. The * operator has multiple usages in C apart from being used for multiplication. So the syntax of creating a pointer in python looks like this:
<datatype> * <variable name>;
The datatype can be any kind of data type supported by C. Like int, float, char, double, etc. Later we will see even pointers to structures also. Since C is a strictly-typed language, so a pointer to an integer can point only to integers or addresses where integers are stored similarly for other data types.
See below code snippet:
Here iptr is a variable that can store address of integer variables and fptr can store the address of float variables. And both iptr and fptr are pointers. It means now we can write code that can store the address of a variable like the one shown below.
The variable iptr in the previous code fragment is a variable that can point to any kind of integer data. It means that it can store the address (starting address) of any integer variable. Running the above code will print the same address two times.
Pointers themselves are variables, it means that they will also occupy some memory and will also have an address. So you can even write something like this &iptr.
Accessing pointer Data:
Once you have created a pointer next job is to access the data. To do that we use the * operator, the same one which is used while creating pointers. Below example shows different usage of * and &.
The previous code prints the value of the variable i using the pointer iptr. This step of getting data from a pointer is called as dereferencing. So we dereference the pointer iptr by writing (*iptr).
Dynamic memory allocation and pointers
In previous code snippets, we saw pointers being used for storing addresses of normal variables or the variables created on stack memory. The main purpose of pointers is to handle dynamic memory allocation and allocation of memory on the heap instead of pointing to stack memory.
So if you don’t know heap and stack memory, then each process that runs has multiple segments of memory and heap and stack memory are two of them.
To understand the difference between them for now, just remember that all variables that you create in a program (integers, floats, chars, etc.) and even your pointer variables are stored on stack memory which has a fixed size. But there is something called as heap memory which can be considered as a free store of memory from where your program can request additional memory at run time when needed and can return it back to the Operating System when not needed.
C provides functions malloc and free to handle dynamic memory management. That is to allocate and deallocate memory while the program is running. And since the memory is requested at run time, hence we cannot have a name assigned to such memory locations. The only way to store and retrieve data from such memory locations is by address and hence pointers come into the picture.