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: Shotgun Ninja 2009-10-30 14:31

Well, technically, it probably should be int *p, because the dereferencing operator (aka the pointer asterisk) is not part of the type, but a per-variable specifier. Think of it like an initialization:

// a and b should both be initialized to 1
// The WRONG way:
//int a, b = 1;

// The RIGHT way:
int a = 1, b = 1;

So basically, you're specifying that both of the variables are pointers, but individually. It's more intuitive for multi-variable declarations as well, which is why it is seen more in C, because the common style for structs uses multi-var declarations wherever it can.

But personally, I prefer int* a. And I'm impartial between char** argv and char *argv[], but the latter just seems unwieldy.

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