|
|
|
|
|
by tordek
5833 days ago
|
|
As said elsewhere: "Declaration follows use". int *foo;
means "foo is a pointer to int". But that's explained different from how it's written. More clearly, you can say that "foo" is an int. Then, doing int *foo;
foo = 50;
is obviously wrong because "foo" is not an int; however int *foo;
*foo = 50;
is correct.Similarly for arrays: int *foo[50];
means "foo is an array of pointers to int", or "foo[5] is an int".From there follows that & is the antithesis of , and they negate each other: int *foo; //foo is a pointer to int.
&*foo; //the address of the contents of foo
foo; // same as above, but shorter.
|
|
In response to the original question, I think the answer is that
is a common way of writing it because that's the way the compiler resolves it. As mentioned elsewhere, is equivalent to so treating the star as part of the type can cause problems.But I agree fully - it makes a lot more sense to me to consider the pointer star as a flag on the type, not a modifier to the name. Just one of the many warts on C and C++ that make me happy I have to use them so infrequently...
[Edit: formatting, stars were getting swallowed when put inline]