Home > Backend Development > C++ > Where Should the Asterisk Go in C/C Pointer Declarations?

Where Should the Asterisk Go in C/C Pointer Declarations?

DDD
Release: 2024-12-11 11:13:11
Original
368 people have browsed it

Where Should the Asterisk Go in C/C   Pointer Declarations?

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;
    Copy after login
  • Variable-adjacent: The asterisk is placed next to the variable name.

    someType *somePtr;
    Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template