Pointers: Declaring with Asterisk Placement
In C and C , pointer declarations vary in style, often causing confusion. The question arises: should the asterisk (*) be placed adjacent to the type name or the variable name?
Placement Conventions
Two primary placement conventions are common:
Type-adjacent: The asterisk is placed next to the type name.
someType* somePtr;
Variable-adjacent: The asterisk is placed next to the variable name.
someType *somePtr;
Significance and Preference
While both conventions lead to the same functionality, the preferred placement depends on personal preference and perceived logicality.
The type-adjacent convention emphasizes the type of the pointer variable, stating "the type of somePtr is a pointer to someType."
Conversely, the variable-adjacent convention emphasizes the type of the pointed-to data, stating "the type of data pointed to by somePtr is someType."
Choice Matters?
The compiler does not distinguish between the two conventions. However, some programmers believe specific placements enhance readability or clarity. Others view it as a matter of style, akin to indentation and brace placement.
Conclusion
Ultimately, the choice between the two placement conventions is subjective. While both are valid and interchangeable, some programmers may find one more logical or aesthetically pleasing than the other. Understanding the underlying semantics and preferences can help inform your coding style and improve communication with other developers.
The above is the detailed content of Where Should the Asterisk Go in C/C Pointer Declarations?. For more information, please follow other related articles on the PHP Chinese website!