Home > Backend Development > C++ > body text

Why is `char *string` preferred over `char* string` in C ?

Susan Sarandon
Release: 2024-11-19 13:36:02
Original
143 people have browsed it

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;
Copy after login

or

char *string;
Copy after login

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;
Copy after login

string1 is a character pointer, but string2 is a single character. For clarity, it's preferable to write:

char *string1, string2;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template