Home > Backend Development > C++ > Which C/C Pointer Declaration Notation is Better: `char* p` or `char *p`?

Which C/C Pointer Declaration Notation is Better: `char* p` or `char *p`?

Mary-Kate Olsen
Release: 2024-12-06 00:14:10
Original
484 people have browsed it

Which C/C   Pointer Declaration Notation is Better: `char* p` or `char *p`?

Pointer Variable Declaration in C/C

In C and C , there are multiple notations for declaring pointer variables, including:

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

The question arises: which notation is more appropriate?

According to Bjarne Stroustrup, the creator of C , the choice between the two notations is a matter of style and emphasis.

Notation (a) emphasizes the syntax of the pointer declaration, focusing on the fact that the asterisk () binds to the variable name. In this view, the declaration reads as "p is what is the int."

Notation (b) emphasizes the type of the pointer variable. It reads as "p is a pointer to an int," highlighting the int* type. This emphasis aligns with the strong emphasis on types in C .

Stroustrup recommends using notation (b) for clarity, especially when declaring multiple pointers in a single line. This notation aligns with the common practice of placing the asterisk next to the variable name to indicate pointer status.

The above is the detailed content of Which C/C Pointer Declaration Notation is Better: `char* p` or `char *p`?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template