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

where do you put the * in type declarations?

Name: Anonymous 2011-08-01 13:57

a) int *p;
b) int* p;
c) int * p;
d) int*p;

c) here

Name: Anonymous 2011-08-02 6:34

If you're programming in C, put the * to right like so:

int *p;

If you're programming in C++, put the * to left like so:

int* p;


If you do anything else, you're doing it wrong.

In C, you tend to think about things more in terms of what it is pointing to. int *p is a pointer to an integer, not so much an integer-pointer.

The reason it makes sense to put the * to left in C++ is largely due to pragmatics, is because the * is a part of type. "p" is an integer-pointer, it's not so much a pointer to an integer.

Furthermore, when you factor in const and volatile qualifiers, placing * to the left becomes more important for nested pointers and references.

int* volatile* const* p;

You now read p's type from right to left. A pointer to a constant pointer to a volatile pointer to an integer.

In C, which traditionally didn't have const, and volatile had slightly different rules, this didn't make so much sense.

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