float* f vs float *f

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Impelluso

    float* f vs float *f


    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


  • Paavo Helde

    #2
    Re: float* f vs float *f

    Tom Impelluso <impellus@attil a.sdsu.edukirju tas:
    >
    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.

    YMMV
    Paavo

    Comment

    • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

      #3
      Re: float* f vs float *f

      On 2008-11-08 18:35, Paavo Helde wrote:
      Tom Impelluso <impellus@attil a.sdsu.edukirju tas:
      >
      >>
      >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

      Comment

      • Zeppe

        #4
        Re: float* f vs float *f

        Erik Wikström wrote:
        On 2008-11-08 18:35, Paavo Helde wrote:
        >Tom Impelluso <impellus@attil a.sdsu.edukirju tas:
        >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

        Comment

        • Rolf Magnus

          #5
          Re: float* f vs float *f

          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.

          Comment

          • red floyd

            #6
            Re: float* f vs float *f

            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.


            Comment

            • Bo Persson

              #7
              Re: float* f vs float *f

              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



              Comment

              • James Kanze

                #8
                Re: float* f vs float *f

                On Nov 8, 11:11 pm, Rolf Magnus <ramag...@t-online.dewrote:
                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:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • Bharath

                  #9
                  Re: float* f vs float *f

                  On Nov 9, 6:29 am, James Kanze <james.ka...@gm ail.comwrote:
                  On Nov 8, 11:11 pm, Rolf Magnus <ramag...@t-online.dewrote:
                  >
                  >
                  >
                  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:james.ka. ..@gmail.com
                  Conseils en informatique orientée objet/
                                     Beratung in objektorientier ter Datenverarbeitu ng
                  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?

                  Comment

                  Working...