A Digital Garden - frequent growth but a bit weedy in the back corner
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 manipulation like this:
#include <stdio.h>
int main(void) {
int a, b;
a = 1;
b = a;
b = 2;
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}If you run this code you’ll see that b is equal to 2 but a is still equal to 1. This is known as value semantics or sometimes copy semantics. When we do int b = a we’re making b store a copy of the value stored in a. Subsequent changes to b will have no effect on a. It kind of makes sense. It’s what you’d expect to happen.
In reference semantics we’d do this instead:
#include <stdio.h>
int main(void) {
int a;
int *b;
a = 1;
b = &a;
*b = 2;
printf("a = %d\n", a);
printf("b = %d\n", *b);
return 0;
}This would result in both a and *b having a value of 2. Here, a is set to 2 indirectly and implicitly through the reference provided by b.
You can read about pointer operators if you don’t understand all those * and & thingies but what’s important here is the idea of references.
In value semantics we’re dealing with objects directly but in reference semantics, a reference provides a link to another thing. The referrer refers to the referent. C pointers are one example of a reference but there are other ways to implement reference semantics - in other words reference is a generic term and pointer is a specific example of a reference.
Referencing in turn is a specific case of indirection, the process of gaining access to one thing via another. The opposite of referencing is dereferencing. There’s more info in my article on C’s pointer syntax
That’s the top and bottom of references really but if you think about them, they do open up your mind to some fairly deep observations about the world. For example here’s a question about numbers.
Finally, here are a couple of subtleties. See if you can answer them yourself before reading my take on them.
What’s the difference between a variable and a pointer? Both are ‘indirections’. The defining difference is that a pointer is a variable whose value is an address which further indirects to a value whereas a variable indirects to a value in a single step.
What’s the difference between an address and a pointer? Not very much really. Both can be used in the same places e.g.
int value;
int i = *(&value);
is equivalent to
int value;
int i;
int *ptr = &value;
i = *ptr;
You can skip the following cat/painting story. But you’re intrigued. So you won’t. But you’re also a contrarian little bastard, so maybe you will. Just because I said you won’t. On the third hand, your raging anxiety at being so clearly seen means you’ll probably contract out the decision making and toss a coin.
One day, while you’re relaxing on the sofa with a beer, all comfy and at peace with the world, the cat wants a chat (if you’re French and confused at your double-take, I make no apologies) and it turns out that the cat, tautologically being a picky little arsehole, who cannot bear to see you at peace, is heartily sick and tired of magnolia. Or so it says. You become somewhat unsettled. In part because the cat is just by default an arsehole, but more particularly because something is clearly a-paw - the cat looks like it is working up to shenanigans. You let it, just once and against your better judgement, shenan, and now it looks like it’s about to shenan yet again. It learns quickly; when it wants to. When it doesn’t? It’s a creature of weapons-grade fabricated ineptitude with a perfectly meted out soupçon (Salut encore les Français! De rien.) of plausible deniability. Arsehole. It’s time to refresh the paintwork.
So you go get the paint - Moroccan Mist, according to the cat, is the dogs bollocks. Which unsettles you again as you’re not sure whether that’s a good thing or a bad thing in the mind of a cat. Obviously you were not supposed to know. But, also obviously, you obviously should have known. Further obviously, you were not accompanied to the shop by the cat who needed, instead, to spend the afternoon licking its own nethers as a soothing trauma response to your attitude.
You paint the walls, alone cos cat == arsehole, late into the night, convinced that ‘Moroccan Mist’ is, in fact, mere marketing wank for ‘magnolia’. But equally convinced that the cat will insist otherwise, with claims of the inferior nature of the human eye and spiked-saccharine, daringly hollow offers to hire some kind of spectroscopic instrumentation to prove its point should you deem its superior faculties unjustifiably untrustworthy.
In the morning, thirty minutes later, awakened by pitiful yet calculated mewling, you discover that the Moroccan Mist in the living room simply will not do. The soft light of dawn has made it clear that it needs to be red. The particular shade of red will have to remain unspecified, as your constant unwillingness to contribute your opinions on matters of such import and instead to rely incessantly on the cat to resolve every conundrum, has become irksome. Apparently, you’re the arsehole. So, despite the insistence of your Internal Ackbar, you have at it.
That evening, the cat returns with half a mouse and an expectation of gratitude. And is annoyed to find that you’ve painted the living room wrong. Because the bedroom is still a disgusting shade of magnolia. You should have painted the living room using referential semantics. Arsehole.