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!