I’m still relatively inexperienced with using pointers in C programming. I have struggled with understanding the concept of pointers for a long time, but recently the pointer logic has clicked and I needed to document it somewhere.

What are pointers in C programming?

A pointer is a variable that points to an address in memory.

Why is it useful to access the memory address?

It is useful because when you create a function in C and send input data to it, it only makes a copy of the value and doesn’t actually change the variable.

So when you send a copy of the address in memory, the function copies the address. Now when the function has the address, it can change the value.

Picture it like this (pointer metaphor):

You are now a warehouse worker. You stock lamps for retail. Now, you need to take a lamp put it in a pretty package, and put it back on the shelf. So what you do is this, you need to find the right lamp (value), so you check the system on where to find it and get the shelf number where it is (address). Then you modify the lamp by putting it in a pretty package and put it back on the shelf where you found it.

This would be the equivalent of changing the value of a variable by reference (using pointers).

In contrast, if you just pass by value (using the variable / not pointers) you would go to the system to find the lamp you need, get an identical lamp, and package it only to throw it in a fire (destroy it).

How to write the signature of a function that passes by reference:

Let’s write a classic function used when learning about pointers – a function that adds 2 numbers and passes the result by reference back to the main function.

void add(int num1, int num2, int *result);

Only the result is a pointer because there is no need to change the values of the numbers.

How to call the function in main():

The important thing here is to make sure that you send the address of the variable you want to change to the pointer argument in the signature. For example:

int main() {

    int a = 3, b = 5, c;
    add(a, b, &c);
    printf("\n C = %d", c);

    return 0;
}

Or, instead of using the ‘&’ to send the address, you could just create a pointer variable to send through. But be careful here – you need to initialize the pointer before you can change the value. Otherwise, you will receive a “segmentation fault” error. This happens when you try to change something in memory that you don’t have access to.

int main() {

    int a = 3, b = 5, c;
    int *ptr;
    ptr = &c;

    add(a, b, ptr);
    printf("\n C = %d", c);

    return 0;
}

Advance your understanding of pointers.

Now that the basic concept of pointers is understood, let’s move on.

Pointers of multiple orders (Pointer to a Pointer to a Pointer to a …)

To understand this it helps to see Pointers as arrays. Arrays are actually natural pointers. When you create an array, the signature without an index is already a pointer without the identifier.

Now, when you think of a pointer, you can just picture an array. Sometimes, it’ll just be an array of one. As for a Pointer to a Pointer, it’s just an array of pointer – a 2D array.

I have mentioned this above but I’ll mention it again because I keep on repeating this mistake: make sure that you have initialized the pointer before you try to assign anything to it. This causes a “segmentation fault”.

It seems ridiculous, but I seem to forget about it more when using 2nd order pointers than regular pointers.

Let’s go back to the analogy of the warehouse. You, the warehouse worker, just got a new shipment of lamps to add to your shelves. In order to know where to put the new lamps, you need to know the shelf ID and the position on the shelf (2nd order pointers / 2D array to save the location).

But, if you forget to initialize the pointers, it’s like assigning the storage location to a shelf in a warehouse that doesn’t exist! No warehouse (initial variable), no shelf (pointer), no storage location (pointer to a pointer). So make sure you build the warehouse first.

Conclusion

Understanding pointers is a big step forward in learning C programming. While they can be tricky at first, with practice and real-world use, they start to make sense.

Try experimenting with your own small projects—build simple programs that pass values by reference, use arrays, or even explore multi-level pointers.

The more you apply these concepts, the more natural they’ll become. Happy coding!

Similar Posts