'int *i ' or 'int* i'

J

Julia

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
 
P

pete

Julia said:
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;
 
A

Andrew Poelstra

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! ;-)
 
K

Keith Thompson

Julia said:
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.
 
K

Kaz Kylheku

Julia said:
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;
 
I

inmatarian

Julia said:
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.
 
K

Keith Thompson

inmatarian said:
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.
 
B

Bill Pursell

Julia said:
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 '*'.
 
K

Keith Thompson

Bill Pursell said:
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.
 
B

Bill Pursell

Keith said:
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);


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

jaysome

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
 
S

SuperKoko

Keith said:
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.
 
I

Ian Gay

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

Ian Collins

Ian said:
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.
 
I

inmatarian

Bill said:
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.
 
P

pemo

Keith said:
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;
 
A

av

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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top