Origin of pointer syntax
I've always been curious why you declare a pointer like this:
you ask for a reference like this:
and you dereference with the same thing you declare a pointer with:
&something; // give me a reference to something
Why is that? What is the commonality between declaring a pointer and dereferencing it?
*something; // give me the value of something
Compare these two declarations:
Originally Posted by dudeomanodude
The first one says, "x is an integer." The second one, although technically declaring a pointer, can be thought of as saying, "*y is an integer." Therefore, y must be "something" which, when you stick a star on the front of it, is an integer. In other words, a pointer to an integer.
So the declaration syntax is borrowing from the dereferencing syntax, not the other way around.
So if you have
then ptr is a pointer-to-int and *ptr is an int, all at the same time.
IOW, they needed something in the declaration to say "this is a pointer", and using the same symbol * as dereferencing allowed the pun above, without using up another keyword or symbol.
(I seem to recall somebody smart and important saying much the same thing but with more authority -- maybe Stroustrup in his FAQ?)
Yep, that clears it up for me.
Originally Posted by brewbuck