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;

There’s just one little wrinkle you need to know about structs when it comes to pointers. If you have a pointer to a struct and you want to get access to one of its fields then there’s a shorthand: ptrToStruct->fieldName is the same as *(ptrToStruct).fieldName

Arrays are contiguous blocks of memory holding multiple objects of the same type. These can be quite large and passing them into functions in the conventional, by value sense, can be quite inefficient both in terms of memory and processor cycles. Fortunately, pointers come to our rescue here by allowing us to pass a pointerRead more »

C is a statically typed language, meaning that we explicitly tell the compiler what type our objects are. For example, in Python, which is dynamically rather than statically typed, we’d write value = 12 whereas in C we’d write int value = 12. What about pointers then? Given that we write int value1 and floatRead more »

« Previous PageNext Page »