Home >Backend Development >C++ >C/C Pointer Declaration: `char* p` vs. `char *p` – Which is Correct?

C/C Pointer Declaration: `char* p` vs. `char *p` – Which is Correct?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-09 03:06:11719browse

C/C   Pointer Declaration: `char* p` vs. `char *p` – Which is Correct?

How to Correctly Declare Pointer Variables in C/C

This question centers around the seemingly conflicting styles of pointer variable declaration in C/C :

(a) char* p;
(b) char *p;

Some individuals prefer notation (a), while others favor (b). This article aims to shed light on the rationale behind each style.

Style (b) emphasizes that the type is character and the variable (p) may point to that character. This is evident in the declaration of multiple pointers:

char* c, *d;

Style (a), on the other hand, suggests that there is a type char* and that the variable (c) is of that type. This can be misleading, as the type is actually char and the memory location pointed to by c is of that type.

Bjarne Stroustrup, the creator of C , offers this insight:

"The choice between 'int p;' and 'int p;' is not about right and wrong, but about style and emphasis. C emphasizes expressions, while C emphasizes types."

Therefore, the decision is ultimately a matter of personal preference and the desired level of emphasis on syntax or type. However, it is recommended to adopt style (b) for consistency when declaring multiple pointers in a single line.

The above is the detailed content of C/C Pointer Declaration: `char* p` vs. `char *p` – Which is Correct?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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