K
Koster
Something I've been puzzled over for a while. This really has no consequence at
all, but I'm curious for the _reason_ behind where we commonly place the
asterisk when declaring pointer variables.
When defining a variable which is of pointer-to-foo type, it seems the
'convention' is to put the asterisk before the variable's identifier, like this:
foo *ptr;
The C standards all seem to use this conventions, but it is equally valid
(AFAIK) to place the asterisk after the variable's type, like this:
foo* ptr;
....or even on its own:
foo * ptr;
Now, am I correct in suggesting that the 'pointerness' of a variable is an
attribute of its type, rather than its identifier? After all, when specifying a
function's arguments in a declaration, we declare the argument's expected type
as pointer-to-foo:
int myFunction(foo *, foo *); /* common */
or int myFunction(foo*, foo*); /* not so common */
The qualified type of the variable is pointer-to-foo. No less than that.
As such, why do we specify the 'pointerness' of a variable by putting the
asterisk by its name, when _really_ it is an attribute of its type? To me it
seems much more logical to put the asterisk with the rest of the type, but maybe
I just think crooked.
So far I have seen only one instance of using the 'conventional' manner which is
more logical than the mentioned alternative, and that concerns pointers to
constant foos (and not mistaking them for constant pointers to foos):
foo *const ptr;
seems to be less ambiguous than
foo* const ptr;
Vice-versa however, it seems the alternative method wins:
const foo* ptr;
makes more sense to me than
const foo *ptr;
I guess though when you've been reading C for more than thirty years and you
have dreams where everyone speaks C, nothing is ambiguous. Just wanting some
opinions.
Koster
all, but I'm curious for the _reason_ behind where we commonly place the
asterisk when declaring pointer variables.
When defining a variable which is of pointer-to-foo type, it seems the
'convention' is to put the asterisk before the variable's identifier, like this:
foo *ptr;
The C standards all seem to use this conventions, but it is equally valid
(AFAIK) to place the asterisk after the variable's type, like this:
foo* ptr;
....or even on its own:
foo * ptr;
Now, am I correct in suggesting that the 'pointerness' of a variable is an
attribute of its type, rather than its identifier? After all, when specifying a
function's arguments in a declaration, we declare the argument's expected type
as pointer-to-foo:
int myFunction(foo *, foo *); /* common */
or int myFunction(foo*, foo*); /* not so common */
The qualified type of the variable is pointer-to-foo. No less than that.
As such, why do we specify the 'pointerness' of a variable by putting the
asterisk by its name, when _really_ it is an attribute of its type? To me it
seems much more logical to put the asterisk with the rest of the type, but maybe
I just think crooked.
So far I have seen only one instance of using the 'conventional' manner which is
more logical than the mentioned alternative, and that concerns pointers to
constant foos (and not mistaking them for constant pointers to foos):
foo *const ptr;
seems to be less ambiguous than
foo* const ptr;
Vice-versa however, it seems the alternative method wins:
const foo* ptr;
makes more sense to me than
const foo *ptr;
I guess though when you've been reading C for more than thirty years and you
have dreams where everyone speaks C, nothing is ambiguous. Just wanting some
opinions.
Koster