Fog Creek Software
Discussion Board




C/C++ Pointer Question

Is there any difference between:

char* p;

and

char *p;

?

Both seem to work, but I'm interested in any semantic "gotchas" or subtle effects.

Thanks!

C++ Beginner
Wednesday, February 25, 2004

There is no syntactic difference between those two statements, only a style difference.  Most C++ programmers I know use type* name rather than type *name to emphasize the fact that name is "a pointer to type."  C programmers tend to prefer type *name, for reasons unknown to me.

John Briggs
Wednesday, February 25, 2004

They're semantically the same... C doesn't care about whitespace in this situation, and generally doesn't unless there is ambiguity.  And there's not here.

It's a matter of style.  Some people will try to tell you that "hardcore" C programmers prefer char  *p, but that's not true at all.

It is "supposed" to be read "*p is a char", so by inference p is a pointer to a char.

But a lot of people (probably most) write it this way:

char*  p

because that implies that the you can just think that the type of p is char*, e.g. p is a character pointer.

But it is technically correct the first way:

char* p, q

declares a pointer to a char p, and a plain char, q.  You have to declare it ilke this:

char *p, *q
char* p, *q

in order for q to be a pointer as well.

Roose
Wednesday, February 25, 2004

I have a suspicion I know why C programmers prefer "type *name" to "type* name". I'm happy to be totally wrong, however.

In C, you've gotta declare all your variables at the start of a function. (Well, technically, you've gotta declare all variables at the start of their scope, but the point remains the same.) In C++, you typically declare a variable as late as possible, and generally do so when you've got something meaningful to initialise a variable with.

As a result, C functions tend to have a lot of variables declared at all once, right up the top of a function.

Rather than typing:

int x;
int y;
int z;

a C programmer will often type

int x, y, z;

When you're declaring pointer variables, the pointer attaches to the variable and NOT the type. So if you type:

char* source, target;

you're declaring a character pointer called source but a character called target. target is NOT a pointer. For that reason, C programmers prefer to visually associated the * with the name, not the type, as in:

char *source, target;

to make it clear that source is a pointer to a character and target is a simple character.

Andrew Lighten
Wednesday, February 25, 2004

This forum needs a "someone else is typing the same explaination as you right this very minute" popup alert.

Andrew Lighten
Wednesday, February 25, 2004

char *x= "@" ;
char *y= " " ;

z = *x/*y ;

S. Tanna
Wednesday, February 25, 2004

Can anyone explain why the creators decided that the * associates with the variable and not the type?

It seems, in my limited viewpoint, that:

char* p, q; //incorrect

is much cleaner than

char* p, *q; //correct

?

Thanks

C++ Beginner
Wednesday, February 25, 2004

I type char *foo in C because that's how I learned it from K&R.

If I ever get around to using C++ one of these days, I might be inclined to use char* foo, just to see if I notice a difference.

SG
Wednesday, February 25, 2004

The * sign sticks to the thing to its right by preference. So when you type (say) "int*p", the * associates itself with the 'p' rather than the 'int'. So the compiler sees "*p, an int" rather than "p, an int *".

Insert half smiley here.
Wednesday, February 25, 2004

And so, which I missed out, the operator is just doing its usual thing rather than being annoying in this specific case. I guess it has to stick to the next thing, because otherwise there would be ambiguity in expressions involving multiplication.

Insert half smiley here.
Wednesday, February 25, 2004

It kind of makes sense:

int *p;

"the thing pointed to by p is an int."


S.Tanna: was that "@"/" " a pun?

Alex.ro
Wednesday, February 25, 2004

For the original rationale, see:

http://cm.bell-labs.com/cm/cs/who/dmr/chist.html

There's also a discussion at:

http://www.research.att.com/~bs/bs_faq2.html#whitespace

as
Wednesday, February 25, 2004

Those are great resources. Thanks!!

C++ Beginner
Wednesday, February 25, 2004

One short and simple reason that *p is associates with the variable there is a single syntax for DECLARATION and EXPRESSIONS.

e.g.

int **ppi;

is a declaration.

**ppi

is an expression (which is an int), and you use can use the same syntax.

If anyone can come up with a clearer explanation of this, go ahead : )

Roose
Wednesday, February 25, 2004

> S.Tanna: was that "@"/" " a pun?

Almost. A comment on the inadequacy of C syntax

Look carefully at the line assigning z, it can be read two ways

S. Tanna
Wednesday, February 25, 2004

All I see is an unterminated comment...

Dennis Atkins
Wednesday, February 25, 2004

Alex.ro: S.Tanna's post is a classic. Let me insert spaces, and read it again: *x /* y;

If it still isn't obvious, copy it to an editor that does Syntax highlighting.

Ori Berger
Wednesday, February 25, 2004

Interesting points here!

I've got used to int *p and that's why I put it similarly
in casts: (int *) and [almost] never (int*).

People like me that worked for others only very
early in carier tend to hold much more to some specific
style of writing code. People that change places must
fit int into established style of others and probably
are not so stuck to their own way of doing things.
Should I say "more flexible"? I'm afraid I wouldn't
even notice that p is pointer looking at this:

int*________p;
PRINTDIALOG pd;

<underline is acting blank here>

VPC
Wednesday, February 25, 2004

I must be weird, I usually write it as:

char * blah

Although strangely enough, I write references as:

char &blah

No real reason behind it, it's just what I find asthetically pleasing and so naturally tend to type.

Sum Dum Gai
Wednesday, February 25, 2004

*  Recent Topics

*  Fog Creek Home