Home > Article > Backend Development > Why is `char *string` preferred over `char* string` in C ?
When to Use char string vs. char string
In C , null-terminated strings are prevalent. This prompts the question: which declaration makes more sense?
char* string;
or
char *string;
Logically, the char* format seems more appropriate, as "string" is a pointer to a character, not a single character. However, the latter format is more common.
The reason stems from the fact that the * goes with the previous identifier. So, in the declaration below:
char* string1, string2;
string1 is a character pointer, but string2 is a single character. For clarity, it's preferable to write:
char *string1, string2;
Additionally, good practice advises against declaring multiple variables in a single declaration, particularly when some are pointers. By separating each declaration, you minimize potential confusion.
The above is the detailed content of Why is `char *string` preferred over `char* string` in C ?. For more information, please follow other related articles on the PHP Chinese website!