views:

759

answers:

13

Duplicate of this question.

I'm learning C++ at the moment, and I'm coming across a lot of null-terminated strings. This has got me thinking, what makes more sense when declaring pointers:

char* string

or

char *string

? To me, the char* format makes more sense, because the type of "string" is a pointer to a char, rather than a char. However, I generally see the latter format. This applies to references as well, obviously.

Could someone tell me if there is a logical reason for the latter format?

+30  A: 

In the following declaration:

char* string1, string2;

string1 is a character pointer, but string2 is a single character only. For this reason, the declaration is usually formatted like:

char *string1, string2;

which makes it slightly clearer that the * applies to string1 but not string2. Good practice is to avoid declaring multiple variables in one declaration, especially if some of them are pointers.

Greg Hewgill
IMHO this should not be considered as THE good answer.Considering that:- We are talking about C++- The type is char*- The variable that you'll pass around is "string", and rarely "*string".- A variable must be declared as late as possible, so by definition not on the same line than another one.- Bjarne says "A typical C++ programmer writes int* p... I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.- const char* const stringThen char* string makes definitely more sense.I even prefer char * string, for clarity.
Jem
+2  A: 

given that you can make multiple declarations on a single line it makes more sense to me that it belongs to the variable rather than the type, e.g.

char *pString, arrString[20];
Simon
I don;t think that is a good thing to do... not very readable
Tim
less error prone since the type corresponds to the variable, regardless of its readability
Simon
+2  A: 

I too think the char* foo makes more sense when declaring a single variable.

The only time I use the other format is when declaring multiple variables on a single line such as:

char *foo, bar;

Dana Holt
+16  A: 

Bjarne Stroustrup has something to say about this:

The critical confusion comes (only) when people try to declare several pointers with a single declaration:

int* p, p1; // probable error: p1 is not an int*
smo
Brilliant! Stroustrup agrees with me! Thanks.
Skilldrick
This is how I've always felt about it.
Nick Presta
+3  A: 

In C++ it makes sense to put the * at the type identifier like char*, as it is part of the type itself.

But if you define multiple variables in the same statement this gets ambiguous and misleading.

char* foo, bar;

While foo has the type char* now, bar has the type char. Therefore many people prefer

char *foo, bar;

to make this clear.

Raim
+3  A: 

Personally I never declare multiple variables on one line.

Because of that and the fact that I really find patterns nice, I find this simple pattern appealing:

type varname

This means I almost always say int[] iArray or int* piArray (or whatever the hungarian notation would be, I haven't used C++ in a decade).

This also means that I never use int iArray[], since the fact that it's an array is part of the type, not the variable name.

Thinking this way has helped me simplify definitions a few times as well.

Just offering another POV

Bill K
+3  A: 

In most cases, std::string str is much preferable to a char *.

That being said, I usually do something like DataType * ptr, to make the * stand out better. Or, much more often, std::tr1::smart_ptr<DataType> ptr, or maybe an auto_ptr.

Very simply, raw pointers are dangerous to use in quite a few ways, and if I'm going to use one I want it to stand out.

David Thornley
+2  A: 

I really like the "char* foo" idiom, because it puts all the information about the variable's type in one place. "char *foo" can be quite confusing if the "*foo" part is tabbed halfway across the screen!

On the other hand, I do agree with other posters that it's confusing when declaring multiple variables on one line. I try not to mix types when declaring multiple variables on one line, and char and char* are separate types in my mind, so that particular situation isn't one that would likely occur in my code.

MattK
The problem is that the C type system doesn't work that way, instead it puts the declared variable or type in the middle. Just try to declare an array or a pointer to a function.
starblue
Yeah, that's true--my philosophy isn't applicable everywhere in C/C++, unfortunately. But I do like to declare pointers and references that way, for clarity.
MattK
+1  A: 

My answer: neither, you should use:

char * string;

Consider, if Dennis Ritchie (designer of the C language) instead of using the '*' character had used a keyword 'ptr', we would all have to be saying:

char ptr string;

and not having these discussions.

It is also, IMHO, more readable.

anon
I don't like this one because it looks more like the multiplication operator...
Skilldrick
+3  A: 

(std::string is better than char* for most uses.)

I always put the * with the type, as in char* because, as Stroustrup noted, this emphasizes the type.

And I never declare variables together on one line to avoid the common gotcha discussed in the responses here;

char* p, q;  // oops, probably not what was meant
// this is mandated by our style guide:
char* p;
char* q;
Brian Neal
+4  A: 

What no one mentioned yet is that the following

char *string

can also be read as declaring the type of the expression *string (read: the indirection operator applied to the pointer string) as char. From this point of view, the notation is perfectly reasonable.

That being said, I use

char * string

myself ;)

Christoph
"That being said, I use char * string" - Me too, I started converging on this format after considering both the 'char* ' and 'char *' formats and wasn't happy with either of them ...
stefanB
+1, useful interpretation of otherwise confusing syntax!
shambulator
A: 

The first suggestion (of char* var) would be fine as long as you only declared a single variable per line, but personally, I prefer the "char *var;" and a single variable per line - it may make the function longer in line count, but is very clear.

Finally, C++ should be using "string var", but we always end up either inheriting code with "char *" or having to connect to non-C++ libraries.

James Fisher
+2  A: 

Technically, it makes sense to write

char *f

Because *f is the declarator in that, while the char is the declaration specifier which specifies a basic type of all the declarators. The declarators contain operators like (), [] and & which can be thought of modifying the base type, and the actual identifier.

I use the above form of putting * to the identifier because it emphasizes that *f actually is the declarator, while char is the basic type.

To a degree, one can think of the modifiers in a declarator to make the type in the declaration specifier when the operators are applied to the identifier. But it does not always work:

int a, *b, &c = a, d(), e[1], C::*f;

Note how all of a, *b, d() and e[1] statically resolve to an int - apart from &c and C::* which declare a reference (thus it needs to be initialized) and a pointer to a class member respectively.

Johannes Schaub - litb
+1 Very well stated.
Amardeep