Gaining Experience with Pointers in C programming

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

  • Gear Essentials: Gear Rack (Rack and Pinion)

    Gear rack or rack and pinion are super useful. Converting rotational motion to linear motion has been so useful in just about every industry.  Whether you’re an engineer, a technician, or just curious about how these systems work, you need to get a handle on gear racks. So how do gear racks really work and…

  • Gear Essentials: Bevel Gear

    Confused about bevel gears? Don’t be. Problem solved! If that didn’t help I put everything you need to know about bevel gears in this article.  How do the gear teeth work together in a bevel gear pair, how are they manufactured, what are their standard sizes, and more?  Without a clear understanding, you can end…

  • Gear Essentials: Miter Gear

    Ever felt confused trying to figure out what exactly a *miter gear* does or why it’s used? Or how is it different to a bevel gear?  If you’re dealing with machinery or projects that require precise gear systems, getting a handle on miter gears is a great help!  Whether you’re trying to understand *straight tooth…

  • 21 Best Engineering project ideas for 2025

    Are you bored at home looking for some engineering project ideas? Then let me help you decide on your next creation! There are so many projects to build and problems to solve, but which ones are interesting, fun, and useful? I have found 21 engineering project ideas for 2025 that promise to entertain you and solve at least one of life’s problems….

  • Engineering Essentials: What is a Gear Train and Why It Matters

    What is a Gear Train? Understanding gear trains is crucial for anyone diving into engineering. Imagine a car that struggles to climb hills or an electric drill that can’t handle tough materials. Frustrating, right? That’s where gear trains come in, ensuring smooth and efficient power transmission. Let’s explore why gear trains and gear ratios are…

  • Gear Essentials: Types of Gears

    The concept of gears is ancient. They were invented in the 3rd century BC. That is a long time for people to experiment and come of with different types of gear that would suit their needs best.  Understanding the types of gears and their uses can simplify your choices when it comes to power transfer, speed control, and torque adjustment in machinery. This article offers gear types…