'int *i ' or 'int* i'

Discussion in 'C Programming' started by Julia, Jul 21, 2006.

  1. Julia

    Julia Guest

    Hi, there,

    In C programming, for pointer, I saw two programming styles:
    one is connecting '*' with variable, like, 'int *i';
    the other is connecting '*' with data type, like, 'int* i'

    I wonder which style is more standard. Is there a rule to follow?

    Thanks in advance,

    Julia
     
    Julia, Jul 21, 2006
    #1
    1. Advertising

  2. Julia

    pete Guest

    Julia wrote:
    >
    > Hi, there,
    >
    > In C programming, for pointer, I saw two programming styles:
    > one is connecting '*' with variable, like, 'int *i';
    > the other is connecting '*' with data type, like, 'int* i'
    >
    > I wonder which style is more standard.


    I think that
    int *i;
    is more common than
    int* i;

    int *i;
    goes along with
    int *i, *j, *k;

    > Is there a rule to follow?


    --
    pete
     
    pete, Jul 21, 2006
    #2
    1. Advertising

  3. On 2006-07-21, Julia <> wrote:
    > Hi, there,
    >
    > In C programming, for pointer, I saw two programming styles:
    > one is connecting '*' with variable, like, 'int *i';
    > the other is connecting '*' with data type, like, 'int* i'
    >


    int *i is better style, IMHO.

    What do you think that
    int* i, j, k, l;
    means? What do you think that the designer intended it to mean?
    int *a, *b, c, *d;
    is very obvious in what it means, although some will debate the
    merits of comining declarations onto one line.

    > I wonder which style is more standard. Is there a rule to follow?
    >


    Style isn't standardized, and anyone attempting to do so would be
    ostricized. It rhymes! ;-)

    --
    Andrew Poelstra <website down>
    My server is down; you can't mail
    me, nor can I post convieniently.
     
    Andrew Poelstra, Jul 22, 2006
    #3
  4. "Julia" <> writes:
    > In C programming, for pointer, I saw two programming styles:
    > one is connecting '*' with variable, like, 'int *i';
    > the other is connecting '*' with data type, like, 'int* i'
    >
    > I wonder which style is more standard. Is there a rule to follow?


    Both are semantically identical by themselves; the compiler doesn't
    care which one you use. But the "int *i;" form more closely follows
    the actual meaning of the declaration.

    C uses a "declaration follows use" principle, which can be confusing
    until you get used to it. For example,

    int *i;

    means "*i is an int", not "i is a pointer to int". (Actually it means
    both, since they're logically equivalent, but the "*i is an int"
    interpretation is the one that corresponds to the syntax.)

    This becomes particularly important when you declare multiple things
    on one line. For example:

    int x, *y, **z;

    means that x, *y, and **z are all of type int (which makes x an int, y
    a pointer to int, and z a pointer to pointer to int).

    Using

    int* x;

    makes it tempting to write:

    int* x, y;

    which doesn't make x and y pointers to int; it makes x a pointer to
    int and y an int, as you can see if you correct the spacing:

    int *x, y;

    And since spaces between tokens are ignored, the compiler won't warn
    you if your layout is misleading; it will simply assume that you meant
    what you wrote.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 22, 2006
    #4
  5. Julia

    Kaz Kylheku Guest

    Julia wrote:
    > Hi, there,
    >
    > In C programming, for pointer, I saw two programming styles:
    > one is connecting '*' with variable, like, 'int *i';
    > the other is connecting '*' with data type, like, 'int* i'
    >
    > I wonder which style is more standard. Is there a rule to follow?


    The syntax of the declaration is, roughly:

    <declaration specifiers> <declararator> { , <declarator> }*

    In other words, it has two main constituents: a list of specifiers,
    followed by a list of comma-separated declarators.

    All of the declarators share the same specifier list.

    The * in *i is part of the declarator syntax, not part of the
    declaration specifiers. So when you write

    int* i;

    the main syntactic division is between "int" and "*". Yet the
    whitespace emphasizes a minor syntactic division.

    Consider this:

    int* p, q; /* programmer wanted p and q to be pointers */

    Here, of course, there are two declarators "*p" and "q", in other
    words:

    int *p, q;
     
    Kaz Kylheku, Jul 22, 2006
    #5
  6. Julia

    inmatarian Guest

    Julia wrote:
    > In C programming, for pointer, I saw two programming styles:
    > one is connecting '*' with variable, like, 'int *i';
    > the other is connecting '*' with data type, like, 'int* i'
    >
    > I wonder which style is more standard. Is there a rule to follow?


    My personal favorite is with a space between on both sides of the
    asterisk. int * i;

    inmatarian.
     
    inmatarian, Jul 22, 2006
    #6
  7. inmatarian <> writes:
    > Julia wrote:
    >> In C programming, for pointer, I saw two programming styles:
    >> one is connecting '*' with variable, like, 'int *i';
    >> the other is connecting '*' with data type, like, 'int* i'
    >> I wonder which style is more standard. Is there a rule to follow?

    >
    > My personal favorite is with a space between on both sides of the
    > asterisk. int * i;


    I don't think you'll find many people sharing your enthusiasm for that
    style.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 22, 2006
    #7
  8. Julia

    Bill Pursell Guest

    Julia wrote:

    > In C programming, for pointer, I saw two programming styles:
    > one is connecting '*' with variable, like, 'int *i';
    > the other is connecting '*' with data type, like, 'int* i'
    >
    > I wonder which style is more standard. Is there a rule to follow?


    Several people have pointed out that
    int *i;
    is the preferred style for variables. However,
    for functions that return an int *, I find that
    int* foo(void);
    is preferrable to
    int *foo(void);

    Anyone who abuses the syntax to write:
    int* foo(void), baz(void);
    (which declares baz as a function that returns an int) should
    be shot regardless of the placement of the '*'.
     
    Bill Pursell, Jul 22, 2006
    #8
  9. "Bill Pursell" <> writes:
    > Julia wrote:
    >> In C programming, for pointer, I saw two programming styles:
    >> one is connecting '*' with variable, like, 'int *i';
    >> the other is connecting '*' with data type, like, 'int* i'
    >>
    >> I wonder which style is more standard. Is there a rule to follow?

    >
    > Several people have pointed out that
    > int *i;
    > is the preferred style for variables. However,
    > for functions that return an int *, I find that
    > int* foo(void);
    > is preferrable to
    > int *foo(void);


    It's a matter of style, and therefore there's no one correct answer.
    But if there were :cool:}, it would be that
    int *foo(void);
    is preferable to
    int* foo(void);

    > Anyone who abuses the syntax to write:
    > int* foo(void), baz(void);
    > (which declares baz as a function that returns an int) should
    > be shot regardless of the placement of the '*'.


    Yes, but combining declarations in one line isn't the only reason to
    put the "*" with the "int"; it's just the most obvious one. It's the
    same declaration-follows-use principle; if we drop the "void", then
    int *foo()
    means that *foo() is an int (and therefore that foo() is a pointer to
    int, and foo is a function returning a pointer to int).

    The compiler doesn't care, but I like the layout to reflect the
    syntax.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 22, 2006
    #9
  10. Julia

    Bill Pursell Guest

    Keith Thompson wrote:
    > "Bill Pursell" <> writes:
    > > Julia wrote:
    > >> In C programming, for pointer, I saw two programming styles:
    > >> one is connecting '*' with variable, like, 'int *i';
    > >> the other is connecting '*' with data type, like, 'int* i'
    > >>
    > >> I wonder which style is more standard. Is there a rule to follow?

    > >
    > > Several people have pointed out that
    > > int *i;
    > > is the preferred style for variables. However,
    > > for functions that return an int *, I find that
    > > int* foo(void);
    > > is preferrable to
    > > int *foo(void);

    >
    > It's a matter of style, and therefore there's no one correct answer.
    > But if there were :cool:}, it would be that
    > int *foo(void);
    > is preferable to
    > int* foo(void);
    >
    > > Anyone who abuses the syntax to write:
    > > int* foo(void), baz(void);
    > > (which declares baz as a function that returns an int) should
    > > be shot regardless of the placement of the '*'.

    >
    > Yes, but combining declarations in one line isn't the only reason to
    > put the "*" with the "int"; it's just the most obvious one. It's the
    > same declaration-follows-use principle; if we drop the "void", then
    > int *foo()
    > means that *foo() is an int (and therefore that foo() is a pointer to
    > int, and foo is a function returning a pointer to int).


    I respect your opinion Keith, but I disagree with this style.
    Consider
    the following 3 blocks:

    void f(int);
    void *g(int, int);
    int func_returns_int(void);
    int **h_int_ptr_ptr(int, int);
    struct foo_long_name fln(int, void *);
    struct foo_long_name *fln_ptr(int, void *);
    BAR gB(int);
    BAR *gB_ptr(int);

    void f(int);
    void *g(int, int);
    int func_returns_int(void);
    int **h_int_ptr_ptr(int, int);
    struct foo_long_name fln(int, void *);
    struct foo_long_name *fln_ptr(int, void *);
    BAR gB(int);
    BAR *gB_ptr(int);

    void f(int);
    void * g(int, int);
    int func_returns_int(void);
    int ** h_int_ptr_ptr(int, int);
    struct foo_long_name fln(int, void *);
    struct foo_long_name *fln_ptr(int, void *);
    BAR gB(int);
    BAR * gB_ptr(int);

    (Which, with variable width fonts all look like crap, but
    in the 2nd two, the function names should be vertically aligned.)

    When looking at the declaration of a function, I've never
    cared about *foo, I've only cared about foo(). I want to
    know what the function returns, and I tend to use the
    3rd style in a block of function declarations, so that
    the return types are easily distinguishable. Now, I'll
    readily acknowledge that nearly everyone on this group
    has more experience than I, so I'd like to see arguments
    against using that 3rd style. (other than the variable
    width font issue, which is totally irrelevant AFAIC)

    Note: I'm ignoring complex function declarations like
    int *(*((*f)(int, int *(*(g)(void *))(void))));
    since I have no clue how to declare that in a
    stylistically meanigful way other than cleaning it
    up with typedefs and macros.
     
    Bill Pursell, Jul 22, 2006
    #10
  11. Julia

    jaysome Guest

    On 22 Jul 2006 00:43:35 -0700, "Bill Pursell" <>
    wrote:

    >
    >Keith Thompson wrote:
    >> "Bill Pursell" <> writes:
    >> > Julia wrote:
    >> >> In C programming, for pointer, I saw two programming styles:
    >> >> one is connecting '*' with variable, like, 'int *i';
    >> >> the other is connecting '*' with data type, like, 'int* i'
    >> >>
    >> >> I wonder which style is more standard. Is there a rule to follow?
    >> >
    >> > Several people have pointed out that
    >> > int *i;
    >> > is the preferred style for variables. However,
    >> > for functions that return an int *, I find that
    >> > int* foo(void);
    >> > is preferrable to
    >> > int *foo(void);

    >>
    >> It's a matter of style, and therefore there's no one correct answer.
    >> But if there were :cool:}, it would be that
    >> int *foo(void);
    >> is preferable to
    >> int* foo(void);
    >>
    >> > Anyone who abuses the syntax to write:
    >> > int* foo(void), baz(void);
    >> > (which declares baz as a function that returns an int) should
    >> > be shot regardless of the placement of the '*'.

    >>
    >> Yes, but combining declarations in one line isn't the only reason to
    >> put the "*" with the "int"; it's just the most obvious one. It's the
    >> same declaration-follows-use principle; if we drop the "void", then
    >> int *foo()
    >> means that *foo() is an int (and therefore that foo() is a pointer to
    >> int, and foo is a function returning a pointer to int).

    >
    >I respect your opinion Keith, but I disagree with this style.
    >Consider
    >the following 3 blocks:
    >
    >void f(int);
    >void *g(int, int);
    >int func_returns_int(void);
    >int **h_int_ptr_ptr(int, int);
    >struct foo_long_name fln(int, void *);
    >struct foo_long_name *fln_ptr(int, void *);
    >BAR gB(int);
    >BAR *gB_ptr(int);
    >
    >void f(int);
    >void *g(int, int);
    >int func_returns_int(void);
    >int **h_int_ptr_ptr(int, int);
    >struct foo_long_name fln(int, void *);
    >struct foo_long_name *fln_ptr(int, void *);
    >BAR gB(int);
    >BAR *gB_ptr(int);
    >
    >void f(int);
    >void * g(int, int);
    >int func_returns_int(void);
    >int ** h_int_ptr_ptr(int, int);
    >struct foo_long_name fln(int, void *);
    >struct foo_long_name *fln_ptr(int, void *);
    >BAR gB(int);
    >BAR * gB_ptr(int);
    >
    >(Which, with variable width fonts all look like crap, but
    >in the 2nd two, the function names should be vertically aligned.)
    >
    >When looking at the declaration of a function, I've never
    >cared about *foo, I've only cared about foo(). I want to
    >know what the function returns, and I tend to use the
    >3rd style in a block of function declarations, so that
    >the return types are easily distinguishable. Now, I'll
    >readily acknowledge that nearly everyone on this group
    >has more experience than I, so I'd like to see arguments
    >against using that 3rd style. (other than the variable
    >width font issue, which is totally irrelevant AFAIC)


    The 3rd style is far superior to the first two for the reasons you've
    stated.

    Personally, I'd change the return types to not have the space before
    the '*'. I like to not have spaces between an identifier and a unary
    operator, but have a space before and after a binary operator. YMMV.

    Regards
    --
    jay
     
    jaysome, Jul 22, 2006
    #11
  12. Julia

    SuperKoko Guest

    Keith Thompson wrote:
    > inmatarian <> writes:
    > > Julia wrote:
    > >> In C programming, for pointer, I saw two programming styles:
    > >> one is connecting '*' with variable, like, 'int *i';
    > >> the other is connecting '*' with data type, like, 'int* i'
    > >> I wonder which style is more standard. Is there a rule to follow?

    > >
    > > My personal favorite is with a space between on both sides of the
    > > asterisk. int * i;

    >
    > I don't think you'll find many people sharing your enthusiasm for that
    > style.
    >

    It is not so unpopular.

    There is also:

    int*i; /* much less popular */

    And any other combination of whitespaces & non-whitespaces.
    And eveybody has his own preferences. :)
    There are probably Good Reasons(TM) explaining why C is not strict
    about whitespaces, indentation, braces positions, and all these
    syntactic details.
     
    SuperKoko, Jul 22, 2006
    #12
  13. Julia

    Ian Gay Guest

    "SuperKoko" <> wrote in
    news::

    >
    > Keith Thompson wrote:
    >> inmatarian <> writes:
    >> > Julia wrote:
    >> >> In C programming, for pointer, I saw two programming styles:
    >> >> one is connecting '*' with variable, like, 'int *i';
    >> >> the other is connecting '*' with data type, like, 'int* i'
    >> >> I wonder which style is more standard. Is there a rule to
    >> >> follow?
    >> >


    I don't like int* i.
    Someday you'll carelessly write
    int* i, j
    and expect j to be a pointer :-(

    >> > My personal favorite is with a space between on both sides of
    >> > the asterisk. int * i;

    >>
    >> I don't think you'll find many people sharing your enthusiasm

    for
    >> that style.
    >>

    > It is not so unpopular.
    >
    > There is also:
    >
    > int*i; /* much less popular */
    >
    > And any other combination of whitespaces & non-whitespaces.
    > And eveybody has his own preferences. :)
    > There are probably Good Reasons(TM) explaining why C is not

    strict
    > about whitespaces, indentation, braces positions, and all these
    > syntactic details.
    >
    >




    --
    *********** To reply by e-mail, make w single in address
    **************
     
    Ian Gay, Jul 23, 2006
    #13
  14. Julia

    Ian Collins Guest

    Ian Gay wrote:
    >
    > I don't like int* i.
    > Someday you'll carelessly write
    > int* i, j
    > and expect j to be a pointer :-(
    >

    Not if your standards prohibit declaring more than one variable on a line.

    I appear to be one of the few who prefer that style, mainly because I
    find it easier to read when there is a block of declarations of
    variables of differing types, like the example posted by Bill Pursell
    elsethread.

    --
    Ian Collins.
     
    Ian Collins, Jul 23, 2006
    #14
  15. Julia

    inmatarian Guest

    Bill Pursell wrote:
    > void f(int);
    > void *g(int, int);
    > int func_returns_int(void);
    > int **h_int_ptr_ptr(int, int);
    > struct foo_long_name fln(int, void *);
    > struct foo_long_name *fln_ptr(int, void *);
    > BAR gB(int);
    > BAR *gB_ptr(int);
    >
    > void f(int);
    > void * g(int, int);
    > int func_returns_int(void);
    > int ** h_int_ptr_ptr(int, int);
    > struct foo_long_name fln(int, void *);
    > struct foo_long_name *fln_ptr(int, void *);
    > BAR gB(int);
    > BAR * gB_ptr(int);


    Not to be a pain in the ass, but I write blocks like this:

    void f(int);
    void * g(int, int);
    int func_returns_int(void);
    int ** h_int_ptr_ptr(int, int);
    struct foo_long_name fln(int, void *);
    struct foo_long_name * fln_ptr(int, void *);
    BAR gB(int);
    BAR * gB_ptr(int);

    Just to demonstrate the space on both sides approach. Style preference,
    it was already commented that this is a less popular form.

    inmatarian.
     
    inmatarian, Jul 23, 2006
    #15
  16. Julia

    pemo Guest

    Keith Thompson wrote:
    > inmatarian <> writes:
    >> Julia wrote:
    >>> In C programming, for pointer, I saw two programming styles:
    >>> one is connecting '*' with variable, like, 'int *i';
    >>> the other is connecting '*' with data type, like, 'int* i'
    >>> I wonder which style is more standard. Is there a rule to follow?

    >>
    >> My personal favorite is with a space between on both sides of the
    >> asterisk. int * i;

    >
    > I don't think you'll find many people sharing your enthusiasm for that
    > style.


    I think it's clearer than the int *i; style, e.g.,

    int *i; // declares a pointer to an int.

    traditionally, when the pointer is dereferenced, it uses the same
    operator/identifier /layout/ ...

    *i; // is an int pointed to by i.

    I personally like the dereference use of *, but don't like K&R declaration
    style. I also don't like tying the * to the type - so ...

    int * i;

    ....

    *i;

    --
    ==============
    Not a pedant
    ==============
     
    pemo, Jul 23, 2006
    #16
  17. Julia

    av Guest

    On 21 Jul 2006 15:28:59 -0700, Julia wrote:
    >Hi, there,
    >In C programming, for pointer, I saw two programming styles:
    >one is connecting '*' with variable, like, 'int *i';
    >the other is connecting '*' with data type, like, 'int* i'
    >
    >I wonder which style is more standard.


    don't know it

    >Is there a rule to follow?


    i follow this (and with this all is ok)
    if function argument or for struct i use
    int* i;
    example
    int* f(int* a, int* b);
    struct{int* a;
    int* b;
    uns z;
    };
    else i use (so in functions vars too)
    int *i;
    expecially if there are more items
    int *i, *j, ***k;
     
    av, Jul 23, 2006
    #17
    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,219
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    457
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    957
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    435
    James Kanze
    Apr 17, 2007
Loading...

Share This Page