So, for work, I may have to brush up on my programming skills in C. C is a relatively powerful language. In fact, it’s powerful enough to really screw up the world if you want.
I don’t object to C because it’s powerful though; I object to it because of its syntax:
How it works now:
int *ptr;
*ptr = 45;
int y = 96;
ptr = &y;
What’s happening here?
In line 1, we create a pointer named ptr, which points to an address.
In line 2, we say 45 lives at that address.
In line 3, we create a number y and set it equal to 96.
In line 4, we set the address of the pointer ptr to the address of y. Think of &y as y’s street address. 96 lives at &y, so when we say that ptr is taking y’s address, it means that ptr now points to 96.
What it means:
You’ll notice that there are only two types of values. There are the left values, the addresses of numbers in memory, and the right values, the actual values of the numbers. When we said ptr = &y, we were setting the left value (the address) of ptr to the same location as the left value of y, so ptr no longer points to 45, it points to 96.
The problem with this is that there are two different ways to specify left values. If we’re using a regular variable, like y, we use the address-of symbol, &. If we’re using a pointer, we use no symbol at all.
Possible solution:
Wouldn’t it be more convenient to just use one symbol whenever we’re referring to the address, and no symbol when we’re referring to what lives at the address?
Right now left values can be represented in 2 ways. But ideally, we could make the left value – the address – be specified by one symbol, *, and have the right value use no symbol at all:
int *ptr;
ptr = 45;
int y = 96;
*ptr = *y;
Notice what’s going on here. We start again by declaring a pointer, ptr.
In line 2, we set its right value to 45. Since this is a right value, we don’t use an *.
Then in line 4, we set the address of ptr to the address of y, so now ptr points to 54. Here we only use * when we’re referring to addresses. There is no need for & at all.
Why isn’t it this way? (this isn’t a rhetorical question)
Examples of translation: