Declaring Pointer Variables in C/C : Syntax Clarification
In the realm of pointers in C and C , there exists a minor syntactic variation that has sparked some debate: the placement of the asterisk (*) in pointer declarations. Some programmers opt for the notation (a):
char* p;
while others prefer (b):
char *p;
What's the rationale behind these two distinct notations?
Bjarne Stroustrup, the creator of C , sheds light on this issue. He explains that the choice hinges not on correctness but on stylistic preferences and programming emphasis.
In C, expressions took precedence over declarations, which were often seen as less important. However, C places a strong emphasis on types.
Adherents of the C programming style favour notation (a), arguing that "*p" represents the dereferenced value of the pointer, aligning with the syntactical structure of the C language.
Meanwhile, proponents of the C programming style prefer notation (b), emphasizing the type of the pointer variable itself. The type of "p" in notation (b) is explicitly identified as "int*", highlighting its role as a pointer to an integer.
From a readability standpoint, notation (b) appears more consistent, especially when declaring multiple pointers in a single line. Placing the asterisk with the variable, as in "char c, d", aligns with the intuition that both "c" and "d" are pointers to characters.
Ultimately, the decision between notation (a) and (b) is a matter of personal preference. However, given the increasing prevalence of C and its emphasis on types, notation (b) is generally recommended to enhance clarity and readability in your code.
The above is the detailed content of C/C Pointer Declaration: `char* p;` vs. `char *p;` – What's the Difference?. For more information, please follow other related articles on the PHP Chinese website!