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

Discussion in 'C++' started by Steven T. Hatton, Aug 26, 2004.

  1. 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
    Steven T. Hatton, Aug 26, 2004
    #1
    1. Advertising

  2. 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
    Carsten Spieß, Aug 26, 2004
    #2
    1. Advertising

  3. 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
    Ioannis Vranos, Aug 26, 2004
    #3
  4. Gianni Mariani, Aug 26, 2004
    #4
  5. 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.
    Steven T. Hatton, Aug 26, 2004
    #5
  6. In message <cgk3he$2t8$>, Ioannis Vranos
    <> 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
    Richard Herring, Aug 26, 2004
    #6
  7. Steven T. Hatton

    Julie Guest

    "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.
    Julie, Aug 26, 2004
    #7
  8. "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
    Denis Remezov, Aug 26, 2004
    #8
  9. Steven T. Hatton

    Howard Guest

    "Steven T. Hatton" <> wrote in message
    news:...
    > 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
    Howard, Aug 26, 2004
    #9
  10. Steven T. Hatton

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
    Phlip, Aug 26, 2004
    #10
  11. * Phlip:
    > * Julie:
    > >
    > > * Steven T. Hatton:
    > > >
    > > > 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.


    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.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 26, 2004
    #11
  12. Sorry about the tinyurl, the URL it refers only works as a link from
    certain web-pages such as Google because the web-page provider (news.24.com,
    don't use it, they're incompetent and/or uncaring folks) has access control.

    Same article elsewhere, namely ABC news:

    <url: http://www.abc.net.au/news/newsitems/200408/s1185358.htm>.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 26, 2004
    #12
  13. "Denis Remezov" <> wrote in message
    news:...
    > "Steven T. Hatton" wrote:

    <<snip>>
    > 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.
    --
    Gary
    Gary Labowitz, Aug 26, 2004
    #13
  14. Steven T. Hatton

    Phlip Guest

    Alf P. Steinbach wrote:

    > * Phlip:


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


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

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Aug 26, 2004
    #14
  15. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "Steven T. Hatton" <> wrote in message news:<>...
    > 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-----
    Tommy McDaniel, Aug 26, 2004
    #15
  16. Howard wrote:

    > 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
    Ioannis Vranos, Aug 27, 2004
    #16
  17. Denis Remezov wrote:

    > 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
    Ioannis Vranos, Aug 27, 2004
    #17
  18. Tommy McDaniel wrote:

    > 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
    Ioannis Vranos, Aug 27, 2004
    #18
  19. Ioannis Vranos wrote:
    >
    > Denis Remezov wrote:
    >
    > > 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? :)
    >


    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
    Denis Remezov, Aug 27, 2004
    #19
  20. Denis Remezov wrote:

    > 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
    Ioannis Vranos, Aug 27, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,626
    Old Wolf
    Jan 20, 2004
  3. jamilah

    Muhammad (pbuh) was a religious teacher

    jamilah, Mar 3, 2010, in forum: C Programming
    Replies:
    0
    Views:
    347
    jamilah
    Mar 3, 2010
  4. Daniel Carrera

    Correction: "religious"

    Daniel Carrera, Sep 5, 2003, in forum: Ruby
    Replies:
    10
    Views:
    193
    Bob X
    Sep 7, 2003
  5. Julian Leviston

    Religious discussion

    Julian Leviston, Feb 25, 2009, in forum: Ruby
    Replies:
    6
    Views:
    110
    Joel VanderWerf
    Feb 26, 2009
Loading...

Share This Page