Pointers in C made simple
When I first came across pointers in C, I thought it was a pretty tricky topic. Even when I was just doing simple pointer exercises, like working with arrays, I didn't really understand what was going on under the hood. Today, I finally took the plunge and dived into chapter 5 of the C book, which explains in detail what pointers are and how to work with them.
I found this example really helpful in understanding pointers better. It goes like this:
#include <stdio.h>
int main()
{
int x = 1, y = 2, z[10]; // Don't worry about z[10] for now;
int *ip;
ip = &x;
y = *ip;
*ip = 0;
ip = &z[0]; // I think this point was added to show that a pointer can also change its value; don't worry about z[10] for now;
return 0;
}
Declaring variables and a pointer
Let's dive in and start declaring some variables! We'll go ahead and create variables: x, y. We also create an ip pointer that will point to a variable of type int, which we'll use to help us keep track of things.
int x = 1, y = 2;
int *ip;
I'd like to draw your attention to the way a pointer is declared: int *ip. This just means that ip is a pointer to a variable of the int type. The asterisk * here is super helpful because it shows us that ip is not just a regular variable, but a pointer! This means that it will store the address of a variable of the int type in memory.
It's really important to understand that the * is referring to ip and not int. I'm sure you'll find this example really helpful in understanding this point:
int *w; k = 1; // w – a pointer to a variable of `int` type; k is just a variable of `int` type;
Let's continue parsing the code from the book...
Assigning the address of a variable to a pointer
Next, we assign the address of the x variable to the ip pointer:
ip = &x;
The & operator before x means “address of the variable x”. Thus, now ip stores the address of the variable x. This is a key part of understanding pointers: a pointer itself does not store the value of a variable, it only stores its address in memory.
Pointer dereferencing
The next step is pointer dereferencing:
y = *ip;
Here, the asterisk * before ip means “the value at the address pointed to by the ip pointer”. Since ip points to x, *ip returns the value of x which is 1. It means *ip equal to x or vice versa.
Therefore, the variable y is assigned the value 1.
Changing the value of a variable through a pointer
Now we will change the value of the variable x through a pointer:
*ip = 0;
This line means “assign the value 0 to the variable pointed to by ip”. Since ip points to x, x becomes 0. That is, the * in *ip works both ways. With *ip you can take out a value, likewise with *ip you can write a value. In detail: *ip is x. Lines printf in C code work the same way:
int x = 1;
int *ip = &x;
printf("%d", x); // 1
printf("%d", *ip); // 1
Important points
-
The pointer itself stores only the address of the variable, not its value. This is an important distinction to understand. A pointer points to a memory location where the value of a variable is stored, but the pointer itself stores only the address of that location.
-
Pointer dereferencing, that is, when we write “asterisk variable pointer” -
*ip, we are accessing the value of the variable pointed to byip. This is called pointer dereferencing. -
We can assign the value of the variable through the pointer. That is, dereferencing works both ways.
-
The asterisk -
*is used to dereference a pointer and the ampersand -&is used to get the address of a variable.
At first glance, pointers in C may seem complicated, but when you begin to understand their essence, they become a powerful tool for memory management.
This example helped me to better understand how pointers work and how they can be used. I hope my experience will help you learn this topic faster.
I also suggest you to read the post The Asterisk * Operator in C.
Published on August 30, 2024