float* f vs float *f

Discussion in 'C++' started by Tom Impelluso, Nov 8, 2008.

  1. Hi!


    I have used both of these
    "float *f"
    and
    "float* f"

    Could someone tell me if one is
    preferred and why? Yes, i know both
    work but it makes me feel uneasy.

    Ditto for:

    FILE* fp
    vs.
    FILE *fp

    I would hope to know if there is a standard
    and the second is just allowed.

    thanks
    tom
     
    Tom Impelluso, Nov 8, 2008
    #1
    1. Advertising

  2. On 2008-11-08 18:35, Paavo Helde wrote:
    > Tom Impelluso <> kirjutas:
    >
    >>
    >> Hi!
    >>
    >>
    >> I have used both of these
    >> "float *f"
    >> and
    >> "float* f"
    >>
    >> Could someone tell me if one is
    >> preferred and why?

    >
    > It's a style issue. If you declare multiple pointers together, then this
    > looks better:
    >
    > float *f, *g, *h;
    >
    > than
    >
    > float* f,* g,* h;
    >
    > On the other hand, if you declare a single thing only, then IMHO
    >
    > float* f;
    >
    > looks better than
    >
    > float *f;
    >
    > Some people (not me) argue that this gives one reason to the idea to
    > always declare only a single thing at a time.


    On the other hand, if you already subscribe to the idea of only one
    declaration per line it becomes natural to write "float* f"; type,
    whitespace, and then the name.

    --
    Erik Wikström
     
    Erik Wikström, Nov 8, 2008
    #2
    1. Advertising

  3. Tom Impelluso

    Zeppe Guest

    Erik Wikström wrote:
    > On 2008-11-08 18:35, Paavo Helde wrote:
    >> Tom Impelluso <> kirjutas:
    >> Some people (not me) argue that this gives one reason to the idea to
    >> always declare only a single thing at a time.

    >
    > On the other hand, if you already subscribe to the idea of only one
    > declaration per line it becomes natural to write "float* f"; type,
    > whitespace, and then the name.
    >


    And then whitespace, '=', whitespace, and initialisation value (possibly
    NULL). Initialising each pointer when it is declared is a good practice.

    Best wishes,

    Zeppe
     
    Zeppe, Nov 8, 2008
    #3
  4. Tom Impelluso

    Rolf Magnus Guest

    Tom Impelluso wrote:

    > Could someone tell me if one is
    > preferred and why? Yes, i know both
    > work but it makes me feel uneasy.
    >
    > Ditto for:
    >
    > FILE* fp
    > vs.
    > FILE *fp
    >
    > I would hope to know if there is a standard
    > and the second is just allowed.


    There is no standard. It seems to me that the first is more common in C++,
    while the second is more common in C, but I've seen both in both languages.
    I've also seen a third form, for the undecided:

    FILE * fp;

    I prefer the first version, since it's more natural to me. The * is part of
    the type, and so it belongs to the type and not the name. The inventors of
    C, howerver, seem to think that the other variant is more natural, since it
    kind of matches with the dereferene operator, and you could say that *fp is
    of type FILE.
     
    Rolf Magnus, Nov 8, 2008
    #4
  5. Tom Impelluso

    red floyd Guest

    Rolf Magnus wrote:
    The inventors of
    > C, howerver, seem to think that the other variant is more natural, since it
    > kind of matches with the dereferene operator, and you could say that *fp is
    > of type FILE.


    That's how I finally grokked pointers in C, wayyyyy back in the day.
     
    red floyd, Nov 9, 2008
    #5
  6. Tom Impelluso

    Bo Persson Guest

    Rolf Magnus wrote:
    > Tom Impelluso wrote:
    >
    >> Could someone tell me if one is
    >> preferred and why? Yes, i know both
    >> work but it makes me feel uneasy.
    >>
    >> Ditto for:
    >>
    >> FILE* fp
    >> vs.
    >> FILE *fp
    >>
    >> I would hope to know if there is a standard
    >> and the second is just allowed.

    >
    > There is no standard. It seems to me that the first is more common
    > in C++, while the second is more common in C, but I've seen both in
    > both languages. I've also seen a third form, for the undecided:
    >
    > FILE * fp;
    >
    > I prefer the first version, since it's more natural to me. The * is
    > part of the type, and so it belongs to the type and not the name.
    > The inventors of C, howerver, seem to think that the other variant
    > is more natural, since it kind of matches with the dereferene
    > operator, and you could say that *fp is of type FILE.


    This fails for C++ references, where

    int i = 42;

    int& r = i;
    and
    int &r = i;

    are equivalent, but we can't say that &r is of type int.

    So to be consistent, you might want to use the style
    type-space-name-initializer whenever possible:

    int& r = i;

    int* p = &i;


    Bo Persson
     
    Bo Persson, Nov 9, 2008
    #6
  7. Tom Impelluso

    James Kanze Guest

    Re: float* f vs float *f

    On Nov 8, 11:11 pm, Rolf Magnus <> wrote:
    > Tom Impelluso wrote:
    > > Could someone tell me if one is preferred and why?  Yes, i
    > > know both work but it makes me feel uneasy.


    > > Ditto for:


    > > FILE* fp
    > > vs.
    > > FILE  *fp


    > > I would hope to know if there is a standard and the second
    > > is just allowed.


    > There is no standard. It seems to me that the first is more
    > common in C++, while the second is more common in C, but I've
    > seen both in both languages. I've also seen a third form, for
    > the undecided:


    > FILE * fp;


    > I prefer the first version, since it's more natural to me. The
    > * is part of the type, and so it belongs to the type and not
    > the name. The inventors of C, howerver, seem to think that the
    > other variant is more natural, since it kind of matches with
    > the dereferene operator, and you could say that *fp is of type
    > FILE.


    That was the original philosophy behind C's declaration syntax;
    you specified the basic type, and then an expression which
    denoted the basic type. It broke, of course, the day they
    introduced typedef's and struct. It broke again when const was
    introduced. In sum, it was an experiment that failed, but that
    we still have to live with. What it does mean is that we get a
    lot of ambiguities between expressions and declarations, and
    that there is one more reason to reject more than one
    declaration per statement.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 9, 2008
    #7
  8. Tom Impelluso

    Bharath Guest

    Re: float* f vs float *f

    On Nov 9, 6:29 am, James Kanze <> wrote:
    > On Nov 8, 11:11 pm, Rolf Magnus <> wrote:
    >
    >
    >
    > > Tom Impelluso wrote:
    > > > Could someone tell me if one is preferred and why?  Yes, i
    > > > know both work but it makes me feel uneasy.
    > > > Ditto for:
    > > > FILE* fp
    > > > vs.
    > > > FILE  *fp
    > > > I would hope to know if there is a standard and the second
    > > > is just allowed.

    > > There is no standard. It seems to me that the first is more
    > > common in C++, while the second is more common in C, but I've
    > > seen both in both languages.  I've also seen a third form, for
    > > the undecided:
    > > FILE * fp;
    > > I prefer the first version, since it's more natural to me. The
    > > * is part of the type, and so it belongs to the type and not
    > > the name. The inventors of C, howerver, seem to think that the
    > > other variant is more natural, since it kind of matches with
    > > the dereferene operator, and you could say that *fp is of type
    > > FILE.

    >
    > That was the original philosophy behind C's declaration syntax;
    > you specified the basic type, and then an expression which
    > denoted the basic type.  It broke, of course, the day they
    > introduced typedef's and struct.  It broke again when const was
    > introduced.  In sum, it was an experiment that failed, but that
    > we still have to live with.  What it does mean is that we get a
    > lot of ambiguities between expressions and declarations, and
    > that there is one more reason to reject more than one
    > declaration per statement.
    >
    > --
    > James Kanze (GABI Software)             email:
    > Conseils en informatique orientée objet/
    >                    Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    Check here: http://www.research.att.com/~bs/bs_faq2.html
    see question: Is ``int* p;'' right or is ``int *p;'' right?
     
    Bharath, Nov 9, 2008
    #8
    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. Andy
    Replies:
    7
    Views:
    6,329
    Roedy Green
    May 10, 2004
  2. Jim West
    Replies:
    4
    Views:
    1,514
    Jeff Schwab
    Jan 16, 2004
  3. Jukka Lehtonen

    operator== (float, float)

    Jukka Lehtonen, Aug 4, 2004, in forum: C++
    Replies:
    5
    Views:
    2,456
    Jukka Lehtonen
    Aug 5, 2004
  4. bd
    Replies:
    0
    Views:
    664
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,650
    James Kanze
    Oct 8, 2009
Loading...

Share This Page