Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A religious question: int* i; /*or*/ int *i;?

Reply
Thread Tools

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

 
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-26-2004
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?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
 
Reply With Quote
 
 
 
 
Carsten Spie▀
Guest
Posts: n/a
 
      08-26-2004
Hello Steven,
On Thu, 26 Aug 2004 02:11:22 -0400, Steven T. Hatton wrote:
>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
--
mail<AT>carsten-spiess.de
 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      08-26-2004
Steven T. Hatton wrote:
> 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
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      08-26-2004
Steven T. Hatton wrote:
....
>
> 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?


There is only 1 right way !

http://tinyurl.com/4bnb7

http://groups.google.com/groups?hl=e...andrew.cmu.edu
 
Reply With Quote
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-26-2004
Carsten Spie▀ wrote:

> Hello Steven,
> On Thu, 26 Aug 2004 02:11:22 -0400, Steven T. Hatton wrote:
>>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 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.
--
"[M]y dislike for the preprocessor is well known. Cpp is essential in C
programming, and still important in conventional C++ implementations, but
it is a hack, and so are most of the techniques that rely on it. ...I think
the time has come to be serious about macro-free C++ programming." -B.S.
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      08-26-2004
In message <cgk3he$2t8$(E-Mail Removed)>, Ioannis Vranos
<(E-Mail Removed)> writes
>Steven T. Hatton wrote:
>> 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; ?


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.)

>> 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;
>


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 .

--
Richard Herring
 
Reply With Quote
 
Julie
Guest
Posts: n/a
 
      08-26-2004
"Steven T. Hatton" wrote:
>
> 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.
 
Reply With Quote
 
Denis Remezov
Guest
Posts: n/a
 
      08-26-2004
"Steven T. Hatton" wrote:
>
>
> 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
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      08-26-2004

"Steven T. Hatton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 ****es 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





 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      08-26-2004
Julie wrote:

> "Steven T. Hatton" wrote:


> > int* i; ?


> 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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments