Skip to main content

Command Palette

Search for a command to run...

[CS Fundamentals] Pointers in C

Updated
5 min read
[CS Fundamentals] Pointers in C
L

Computer Science Enthusiast with a Drive for Excellence | Data Science | Web Development | Passionate About Tech & Innovation


Introduction

In this article, I will be talking about pointers in C. Pointers might be a bit hard concept for beginners, but they are essential to understand what your programs are doing. Moreover, there are circumstances where we can use pointers to tackle problems more easily. I am sure that you will have much better understanding after reading this article. So, let's get started.


What Is a Pointer?

As the name suggests, pointers are "pointers". They point to something, and that thing is the address. In C, every variable is allocated to a stack memory. What that means is, there will be an address allocated for each variable. For example, you live in Sydney, and everyone lives in a certain house. Each house has its own address. In this example, house is the variable and address is the address! Note that you can live in many different types of accommodation, such as apartments, houses or dormitories. We will be talking about why types matter later.

Now, let's look at some examples to better understand what a pointer is.

int a = 10;

Let's say we have a variable a, and it holds a value of 10. Let's say its address is 1000. We can now use a pointer to point to a.

int a = 10;
int *b = &a;

What is happening here? What are all those asterisk(*) and & symbol mean? Let me explain step by step.

  1. By having int *b, you are telling C that you are going to use a pointer that points to something with int data type.

  2. By writing &a, you are assigning the address of a to b.

That's all. Now let's visualize this code step by step.

int a = 10;
int *b = &a;

Then, this is the same as

int a = 10;
int *b = 1000;

If you print,

printf("b: %d\n", b); // b: 1000

That simple. Now, what if you actually want the value, not the address of a? That's called dereferencing. You can think of it as unpacking the variable's address. For example, think of how pizzas get delivered to you.

  1. Restaurant makes a pizza

  2. Pack up the pizza

  3. You unpack the pizza to eat it.

This is the same in pointer.

  1. You make the variable (and maybe assign the value as well).

  2. Your new variable points to that variable and get the address.

  3. You dereference it.

In code, it looks like this:

printf("Address: %d\n", b); // Address: 1000
printf("Dereferenced: %d\n", *b); // Dereferenced: 10

As you can see, we can dereference b by having asterisk in front. This might be very confusing because I said that asterisk is to let C know that we are declaring a pointer. You are absolutely correct, and I will show more examples to help you with this concept.

It is right that asterisk has different usages depending on where it is used. If it is used in the variable declaration, it means that we are declaring a pointer. If it is used with the variable that is already assigned an address, that means we are dereferencing (or unpacking).

If it is confusing, it might be a good idea to do this:

int a = 10;
int *b = NULL;
b = &a;

The code above is the same as below.

int a = 10;
int *b = &a;

Now, let's combine all the things we learned so far to see how pointers work in a function. Let's say that we have this:

void print_age(*pAge) {
    printf("pAge: %d\n", pAge)
}

int main() {
    int age = 21;
    int *pAge = &age;

    print_age(pAge);
    return 0;
}

Now, let me show you how this code works. We will set the address of age to 1004 because the actual address set by computer is too long and complicated.

So, we have

void print_age(*pAge) {
    printf("pAge: %d\n", pAge)
}

int main() {
    int age = 21;
    int *pAge = 1004;

    print_age(1004);
    return 0;
}

We can visualize that we are passing the address of age into the function print_age(). Let's continue with our code visualization

void print_age(*1004) {
    // But if you dereference *1004, it is 21
    printf("pAge: %d\n", pAge) // pAge: 21
}

int main() {
    int age = 21;
    int *pAge = 1004;

    print_age(1004);
    return 0;
}

As I have left my comments in the code, *1004 is the same as 21 because the value that is held in the address 1004 is 21.

Now, what if you do this?

int main() {
    int age = 21;
    char *pAge = &age; // <-- is this valid?

    print_age(pAge);
    return 0;
}

char *pAge = 1004 will cause an error because you assigned &age, and that address of age stores the value of 21, which is an integer. So the type must be the same. For example, if your accommodation type is house, you can't point to the apartment because it is a different type.