Skilldrick Skilldrick - 3 months ago 13
C++ Question

What makes more sense - char* string or char *string?


Possible Duplicate:

In C, why is the asterisk before the variable name, rather than after the type?




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?

Answer

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.

Comments