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 15:56

When I see "int *p;" i think "when i dereference p I get an int" or more generally "p gets dereferenced to an int"

Analogously "int factorial(int)" means "factorial gets evaluated with the arguments (int) and becomes int"

And by the same reasoning something like "int *(*q)[5](char *)" (which would confuse 99% of the "HURR, int* p" people) means "when I dereference q I get an array of 5 functions which when evaluated with an 'char *' argument, I will be left with a pointer to an int"

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