In C, we define a variable by preceding it with a data type, thus:
int target; // target is a variable of type integer
This is not difficult to grasp. The syntax is awkward, yes, but Dennis Ritchie skipped out on some Literature classes in college apparently, so this is the historical legacy we have to deal with.
Now to the issue of pointers. A pointer is declared thusly:
int *arrow; // arrow is a variable of type int pointer
Okay, great. We've got a pointer. It doesn't point to anything though. We can fix that, but this is where the mystery starts. I've 'pointed' arrow to target in two different ways. Which one is correct, and why?
*arrow = ⌖ // & is required as an address operator arrow = ⌖ // notice the * prefix has been omitted
In the above example, we are attempting to point the 'arrow' variable at the 'target' address. In the next example, we would like to assign an integer value to 'arrow'. Two different ways of typing it in, which one is correct and why?
*arrow = 3; arrow = 3;
This is why it's important to raise your children to read novels, literature and other forms of creative writing. They may be writing the next programming language and your grandchildren will sacrifice precious brain cells trying to decipher needlessly cryptic syntax. My last question: Really?
6 comments: