So if pointers are so great, why not only use pointers? For two reasons, both of which are consequences of the fact that pointers are a method of indirection. Indirection is, at heart, adding a step to the set of steps needed to get at the information you need. This makes the process of gettingRead more »

Because of C’s pass by value semantics, passing data into functions using parameters requires pushing and popping that data onto and off the stack, byte by byte. Using return values to pass data out of a function back to the callee also requires copying the data onto the stack. If you’re just passing or returningRead more »

So, as a result of some aberrant brain misfiring you’ve decided to start learning to code in C. You pretty quickly come across the idea of functions and your mind is blown. Then you try to write a function and all is not well. Not well at all. In fact things are looking a bitRead more »

Suppose you’re writing a function to double a given number. You might naively assume that this would be a reasonable solution: If you run it you’ll find number remains unchanged. This is, you might be surprised to discover, by design. It’s down to C’s pass by value semantics. Instead, use referential semantics as follows toRead more »

If you’ve run into the classic noob C programmer’s problem of “Why won’t my function update my variable?” then you might have resorted to using a global variable instead of passing the variable into the function as a parameter. Global variables allow ‘write access’ to number from both the caller and the callee, like this,Read more »

The obvious answer is “quite a lot”. There’s the three on the front of my house, there are a couple more in my phone number and there’s one on the number plate of the car across the street from me right now. There are also several copies of my house number three, stored in theRead more »

This is going to be a bit abstract but it’s honestly really worth trying to get your head around if you want a good solid foundation in pointers and referential semantics in general. Pretty much everybody starts their programming career by doing some Hello World stuff and then moving on to some simple variable manipulationRead more »

Pointers are variables that point to a pointee. Like so: Behind that deceptively simple definition lurk some pretty hairy pitfalls and subtleties but it’s still the best place to start learning. In computing, when we’re talking about locations to point to and/or from, we’re really talking about memory addresses. So, given that we’re talking aboutRead more »

The top-level answer to this is that pointers often lead to smaller and faster code. As answers go, that’s not very enlightening though so here are a few concrete examples. To allow functions to change data owned by their caller. This allows you to avoid using global variables for changing data within functions. It alsoRead more »

In addition to the familiar pointers to standard types like ints, floats, arrays and structs, we can also have pointers to pointers: int i = 1; int *ip = &i; int **pp = &ip;

Next Page »