A religious question: int* i; /*or*/ int *i;?

S

Steven T. Hatton

I'm reading through Kyle Loudon's _C++_Pocket_Reference_ for the sake of
review. He uses the notational form used by Kernighan and Ritchie in
declaring pointers (and by extension references). That is, the '*' or '&'
is placed directly preceeding the identifier being declared. Stroustrup
consistently uses the alternative form of placing the '*' or '&'
immediately after the type name of the object to be pointed to or
referenced.

I much prefer Stroustrup's style because it seems to be grammatically more
accurate, and does not give the impression that the '*' or '&' is being
used as an operator in the declaration.

I have seen a third approach of putting whitespace on either side of the '*'
or '&'. To me this is simply equivocating non-committal.

So I have two questions about this. First which do you prefer and why?
Second, what is the formal grammatical decomposition of a declaration of
the form:

int* i; ?

Yes, I am asking this as both a serious question, and tongue-in-cheek. ;)
I'm just curious what others have to say about it. What style do other
notable authorities use?
 
C

Carsten Spieß

Hello Steven,
I much prefer Stroustrup's style because it seems to be grammatically more
accurate, and does not give the impression that the '*' or '&' is being
used as an operator in the declaration.
I also prefer Stroutsroups style, but there is a pitfall with doing
this. Declare
int* i,j;
and you will get
int* i; // as excpected
int j; // an int and not a pointer to int

Regards
Carsten
 
I

Ioannis Vranos

Steven said:
I'm reading through Kyle Loudon's _C++_Pocket_Reference_ for the sake of
review. He uses the notational form used by Kernighan and Ritchie in
declaring pointers (and by extension references). That is, the '*' or '&'
is placed directly preceeding the identifier being declared. Stroustrup
consistently uses the alternative form of placing the '*' or '&'
immediately after the type name of the object to be pointed to or
referenced.

I much prefer Stroustrup's style because it seems to be grammatically more
accurate, and does not give the impression that the '*' or '&' is being
used as an operator in the declaration.

I have seen a third approach of putting whitespace on either side of the '*'
or '&'. To me this is simply equivocating non-committal.

So I have two questions about this. First which do you prefer and why?
Second, what is the formal grammatical decomposition of a declaration of
the form:

int* i; ?

Yes, I am asking this as both a serious question, and tongue-in-cheek. ;)
I'm just curious what others have to say about it. What style do other
notable authorities use?


Consider this:


int *p, i;


and

int* p, i;



i in both cases is an int, so placing the asterisk close to int may be
confusing.


On the other hand, in the first approach * goes with the identifier p
which is a pointer.


So I consider the first one to be the more "rational correct". Also how
would be the second approach in the case:


int *p, i, *r; ?



int* p, i, * r;



The second looks like idiotic to me. :)






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
S

Steven T. Hatton

Carsten said:
Hello Steven,

I also prefer Stroutsroups style, but there is a pitfall with doing
this. Declare
int* i,j;
and you will get
int* i; // as excpected
int j; // an int and not a pointer to int

Regards
Carsten

I realized that after I sent the original message. I don't belive it will
change my style, but it certainly gives weight to the K&R choice.
 
R

Richard Herring

declaration: block-declaration
block-declaration: simple-declaration
simple-declaration: decl-specifier-seq init-declarator-list ";"
decl-specifier-seq: decl-specifier
decl-specifier: type-specifier
type-specifier: simple-type-specifier
simpl-type-specifier: "int"
init-declarator-list: init-declarator
init-declarator: declarator
declarator: ptr-operator declarator
ptr-operator: "*"
declarator: direct-declarator
direct-declarator: declarator-id
declarator-id: id-expression
id-expression: unqualified-id
unqualified_id: identifier
identifier: nondigit
nondigit: "i"

(listing just the relevant branches, and omitting anything subscripted
"opt" that isn't actually used.)
Consider this:


int *p, i;

and

int* p, i;

i in both cases is an int, so placing the asterisk close to int may be
confusing.

On the other hand, in the first approach * goes with the identifier p
which is a pointer.

So I consider the first one to be the more "rational correct". Also how
would be the second approach in the case:

int *p, i, *r; ?

int* p, i, * r;

If you only declare -- and initialise -- variables at the point of first
use, most of these multiple declarations vanish, so the question doesn't
arise ;-).
 
J

Julie

Steven T. Hatton said:
I'm reading through Kyle Loudon's _C++_Pocket_Reference_ for the sake of
review. He uses the notational form used by Kernighan and Ritchie in
declaring pointers (and by extension references). That is, the '*' or '&'
is placed directly preceeding the identifier being declared. Stroustrup
consistently uses the alternative form of placing the '*' or '&'
immediately after the type name of the object to be pointed to or
referenced.

I much prefer Stroustrup's style because it seems to be grammatically more
accurate, and does not give the impression that the '*' or '&' is being
used as an operator in the declaration.

I have seen a third approach of putting whitespace on either side of the '*'
or '&'. To me this is simply equivocating non-committal.

So I have two questions about this. First which do you prefer and why?
Second, what is the formal grammatical decomposition of a declaration of
the form:

int* i; ?

Yes, I am asking this as both a serious question, and tongue-in-cheek. ;)
I'm just curious what others have to say about it. What style do other
notable authorities use?

Well then, I'm 'non'committal' --

Space on both sides, makes it immediately obvious (to me) what is going on,
doesn't crowd the type or name, and I never have multiple declarations on one
line.

Been doing this since I started programming and never had a problem.
 
D

Denis Remezov

Steven T. Hatton said:
I have seen a third approach of putting whitespace on either side of the '*'
or '&'. To me this is simply equivocating non-committal.

So I have two questions about this. First which do you prefer and why?
Second, what is the formal grammatical decomposition of a declaration of
the form:

int* i; ?

I very strongly prefer
int* pa;

There are plenty of C++ (not only C) people who have strong preference to
the contrary. See, e.g., the link posted by Gianni Mariani - we didn't
quite get into a flame war, but we were close; you can read there about
our respective reasons, too.

Take a look at
http://www.research.att.com/~bs/bs_faq2.html#whitespace

if you haven't done so already. I couldn't say it better about
"emphasizing type" vs. "emphasizing syntax".

(Note that I do, in a way, emphasize syntax by using the prefix 'p')


A couple more remarks:

I concede that the following is often a source of confusion and
possible errors:

int* pa = 0,
pb = 0;

To me, however, the alternative

int *a = 0,
*b = 0;

is just as bad because "*b = 0;" reads almost exactly as an independent
assignment expression statement (for a previously declared b), which has
a completely different meaning to "int *b=0;".
Of course, you could avoid multiple declarations in the first place.


Nothing is perfect, and C arrays and function pointers are two cases
where the "T*" way doesn't quite fit, but the syntax for these
could (/"should") have been defined differently in C++ from the way
it is in C. I believe there is a discussion about this in DnE, but
I'm sorry if I'm mistaken (I don't have the book handy).

Denis
 
H

Howard

Steven T. Hatton said:
I'm reading through Kyle Loudon's _C++_Pocket_Reference_ for the sake of
review. He uses the notational form used by Kernighan and Ritchie in
declaring pointers (and by extension references). That is, the '*' or '&'
is placed directly preceeding the identifier being declared. Stroustrup
consistently uses the alternative form of placing the '*' or '&'
immediately after the type name of the object to be pointed to or
referenced.

I much prefer Stroustrup's style because it seems to be grammatically more
accurate, and does not give the impression that the '*' or '&' is being
used as an operator in the declaration.

I have seen a third approach of putting whitespace on either side of the '*'
or '&'. To me this is simply equivocating non-committal.

So I have two questions about this. First which do you prefer and why?
Second, what is the formal grammatical decomposition of a declaration of
the form:

int* i; ?

Yes, I am asking this as both a serious question, and tongue-in-cheek. ;)
I'm just curious what others have to say about it. What style do other
notable authorities use?

This has been discussed so many times it's ridiculous. But, seeing as I'm
the ultimate "notable authority" (from my perspective, anyway :)), I
declare that the CORRECT way is to put the * with the type, not with the
variable name. Plus, NEVER declare more than one variable on one line!
(That prevents any chance of confusion, and makes finding the variable
you're looking for much easier.)

One other thing I do (which I just know pisses some others off) is to always
precede pointer variable names with the small letter 'p', as in pHead. It's
the only time I use that weird hungarian notation, but it really helps me
recognize pointers from non-pointers. (But that makes me wonder...should a
pointer to a pointer to a Head then be a ppHead? Sorry, bad pun.)

"But that's just my opinion. I could be wrong."

-Howard
 
P

Phlip

Julie said:
"Steven T. Hatton" wrote:
Space on both sides, makes it immediately obvious (to me) what is going on,
doesn't crowd the type or name, and I never have multiple declarations on one
line.

Been doing this since I started programming and never had a problem.

Absitively. Space on both sides: int * i.

I'm visually impaired - especially when I program while f---ed up - so every
little visual cue helps.
 
A

Alf P. Steinbach

* Phlip:
* Julie:

Absitively. Space on both sides: int * i.

I'm visually impaired - especially when I program while f---ed up - so every
little visual cue helps.

Although Australian farmers have found that new technology allows
f--king while farming, see <url: http://tinyurl.com/5btbx>, it's generally
not recommended to f--k while programming, especially not in C++, even if
you have space on both sides and can rely on visual cues.
 
G

Gary Labowitz

Denis Remezov said:
"Steven T. Hatton" wrote:
is just as bad because "*b = 0;" reads almost exactly as an independent
assignment expression statement (for a previously declared b), which has
a completely different meaning to "int *b=0;".
Of course, you could avoid multiple declarations in the first place.

Looks like a reason to use
int *b=NULL;
for declaration. I'm fond of int* myself, but I use either with no real
hang-up.
 
P

Phlip

Alf said:
* Phlip:

Although Australian farmers have found that new technology allows
f--king while farming, see <url: http://tinyurl.com/5btbx>, it's generally

I thought that was New Zealand farmers...
not recommended to f--k while programming, especially not in C++, even if
you have space on both sides and can rely on visual cues.

No no no - I'm talking about getting f---ed up while programming. Other end
of the body.

Good style guides, unit tests, etc. help me remain quite productive, thank
you...
 
T

Tommy McDaniel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Steven T. Hatton said:
So I have two questions about this. First which do you prefer and why?

int *ptr. Consider stuff like "int *var1, var2, **var3, var4" or "int
var1, *var2, var3, ***var4". Whether each variable is a pointer or not
(or a pointer to a pointer, or a pointer to a pointer to a pointer...)
is a per-variable thing, not something that applies to the entire line
like int. If you put the asterisk of the first variable with the int,
the rest of the variables just look hokey, whether they are pointers
with their own asterisks next to them or regular ints which now kind
of look like pointers because you stuck the asterisk right with the
int. But of course, this is just my taste, although I think it is a
pretty consistent taste.

Tommy McDaniel
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBLk7ZVB8FYP9YqDcRAt5DAJ9kXZ18V1nKNc6gNuUE6PyG7lwVewCePRGW
LoCe2rR2FmGIFSpn5OlzSbg=
=+h/n
-----END PGP SIGNATURE-----
 
I

Ioannis Vranos

Howard said:
This has been discussed so many times it's ridiculous. But, seeing as I'm
the ultimate "notable authority" (from my perspective, anyway :)), I
declare that the CORRECT way is to put the * with the type, not with the
variable name. Plus, NEVER declare more than one variable on one line!
(That prevents any chance of confusion, and makes finding the variable
you're looking for much easier.)


However if you declare many variables in the same line, the * with the
variable name is the more reasonable and obvious. Also when you declare
a pointer alone, the * going with the variable name is the more
reasonable too. :)


How do you dereference by the way * p=7; or *p=7; ?





One other thing I do (which I just know pisses some others off) is to always
precede pointer variable names with the small letter 'p', as in pHead. It's
the only time I use that weird hungarian notation, but it really helps me
recognize pointers from non-pointers. (But that makes me wonder...should a
pointer to a pointer to a Head then be a ppHead? Sorry, bad pun.)


That's naming conventions and it is another thing that we can quibble
on, in the future.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
I

Ioannis Vranos

Denis said:
A couple more remarks:

I concede that the following is often a source of confusion and
possible errors:

int* pa = 0,
pb = 0;

To me, however, the alternative

int *a = 0,
*b = 0;

is just as bad because "*b = 0;" reads almost exactly as an independent
assignment expression statement (for a previously declared b), which has
a completely different meaning to "int *b=0;".
Of course, you could avoid multiple declarations in the first place.


What about


int *pa=0, pb=0;


which is the most elegant and accurate? :)






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
I

Ioannis Vranos

Tommy said:
int *ptr. Consider stuff like "int *var1, var2, **var3, var4" or "int
var1, *var2, var3, ***var4". Whether each variable is a pointer or not
(or a pointer to a pointer, or a pointer to a pointer to a pointer...)
is a per-variable thing, not something that applies to the entire line
like int. If you put the asterisk of the first variable with the int,
the rest of the variables just look hokey, whether they are pointers
with their own asterisks next to them or regular ints which now kind
of look like pointers because you stuck the asterisk right with the
int. But of course, this is just my taste, although I think it is a
pretty consistent taste.


At last, someone who is reasonable. :)






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 
D

Denis Remezov

Ioannis said:
What about

int *pa=0, pb=0;

which is the most elegant and accurate? :)

Whichever we happen to like.

Most generally, you cannot declare multiple names of /different/
types in one declaration.

int signed s, unsigned u; //illegal

This is why I see no value (or elegance) in being able to declare
variables of type T and T* (and T**, ...) in one declaration.

Denis
 
I

Ioannis Vranos

Denis said:
Whichever we happen to like.

Most generally, you cannot declare multiple names of /different/
types in one declaration.

int signed s, unsigned u; //illegal

This is why I see no value (or elegance) in being able to declare
variables of type T and T* (and T**, ...) in one declaration.


You have got the main issue. Since int and int * are different types the
standard should require an error. That's why we are here with this
syntax stuff today.


However based on the fact that they are allowed to be declared together,
and the asterisk denotes a pointer variable, while an object next to it
in the same declaration without an asterisk becomes a non-pointer
object, the most rational thing in this kind of declarations is the
asterisk to go close to the pointer variable itself rather than the type.






Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top