Is char* myString
or char *myString
considered best practice? My professor consistently uses char* myString
and this seems to be less confusing than char *myString
, but I've also seen many developers put the *
with the string

- 8,874
- 32
- 104
- 192
-
1The early C prophets (K & R) used `char *mystring`. Thou shalt follow the path of your ancestors. – michaelmeyer Apr 08 '14 at 20:25
4 Answers
Some prefer char *myString
because the pointer is not "distributive". That is:
char* str1, str2;
// Is equivalent to:
char* str1; // or char *str1;
char str2; // Note, not a pointer
By defining them in this way:
char *str1, str2;
it is more clear that one is a pointer and one is not. Also note, some coding style guidelines discourage the use of declaring multiple variables on the same line, so this doesn't really matter, and in this scenario (I believe) char* str;
is more readable.

- 9,530
- 20
- 42
-
That's actually a pretty good reason to me. More readable and less error-prone even for a seasoned programmer who has few time and an horrible amount of work to do (and thus quick reviews a code) – Marco A. Apr 08 '14 at 22:43
This is completely up to the programmer's taste. I actually put it in the middle, with spaces on both sides.
char * myString;
But I fear this is one of those endless discussions, such as where to put the opening '{' for an if statement...
My suggestion is to just pick one you like and be consistent with it. Make sure you use it uniformly throughout your code base.

- 4,371
- 2
- 23
- 49
Both are acceptable. However, char*
is just more error-proofing.

- 6,895
- 7
- 45
- 67
-
Though `char* a,b; ` and `char *a,b;` are both the same, and they declare one char pointer and one char. (not two char pointers) – nos Apr 08 '14 at 20:23
as for me; best practice is to put the asterisk before the variable name: suppose you want to declare two pointers; then you will do this:
char *a, *b;
and you will avoid this error:
char* a, b;

- 35,025
- 12
- 111
- 136