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:22

OP here again, I didn't notice there was a page two...

>>41
When I see "int *p;" i think "when i dereference p I get an int" or more generally "p gets dereferenced to an int"
This is the best explanation I've seen, but it still seems silly to me. Isn't this hard to visualize when you have to do pointer arithmetic? Why not abstract away the dereferencing of it?

It also breaks down if you write "int **p". Do you think "When I dereference p I get something that when I dereference it I get an int"? That seems a very convoluted way of understanding p.

>>56
The complexity is irrelevant; it doesn't break at all unless you try to define multiple variables in one declaration.

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