Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

int* p vs. int *p

Name: Anonymous 2009-10-29 0:18

What do you think about the difference between the following declarations? Which do you prefer?

int* p;
int *p;

It's often said the first is C++ style and the second is C style. I prefer C but conceptually the first declaration makes a lot more sense to me. You're defining a variable p of type int*. p is an int*. You dereference it, *p, when you want the int. Makes sense. The second example to me seems like gobbledygook, but it's the way the C authors thought of it, because of this bullshit:

int *p, *q;

So what the fuck is the deal? Can someone who uses "int *p" syntax explain to me what goes through your head when you write that garbage?

Name: Anonymous 2009-10-29 20:12

>>2
>>33
OP here, and that's the problem, you haven't defined anything named *p. *p is a compound expression. You've defined p.

You certainly haven't defined any int; the int it points to need not even exist.

>>4
Obviously I know variable declarations are read right-to-left. It doesn't magically explain why the space is in that arbitrary position. What about "p is a pointer to an int" says that the space goes between the * and int? It's not the "to a", because then why would you write "int **p"?

However if you write "int* p", then it does make sense, because in "p is a..." the "is" is the explanation for the space; it separates the name you're defining from its complete type.

>>3
What the fuck is your post supposed to mean? Do you mean "int* p, q;" is confusing? When you know the standards there's nothing confusing about it, but writing that on purpose in my opinion is deliberately misleading; it's like using the wrong indentation. I'm obviously not suggesting you use the C++ style in defining multiple variables in the same declaration; you are pretty much forced to declare them separately in this case, otherwise the declaration looks wrong (as in >>10).

Did you mean "int* p,q" would be confusing if it worked as you would expect from the spacing? Because if it did, there would be nothing confusing about it (and even less so when you add type modifiers; if it worked this way, stupid expressions like "const int* p, *const q = r;" would actually be invalid. As much as I like C, this is another good reason why the C convention is fucking stupid.)

>>16
Um, this has the exact same problem. Did you know the style "typedef int *PINT" is pervasive throughout the Win32 headers?

>>21
It's not duplicate syntax; it's just whitespace. You can write int*x if you want to.

>>23
Yes, the convention makes this possible, but the compound declaration rules were made because the convention existed in the first place. What was the reasoning behind them then?

>>30
Are you going to explain then why "int *p" makes sense? Because I have yet to see a rational explanation for it.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List