Home > Backend Development > C++ > C/C Pointer Declaration: `char* p;` vs. `char *p;` – What's the Difference?

C/C Pointer Declaration: `char* p;` vs. `char *p;` – What's the Difference?

Mary-Kate Olsen
Release: 2024-12-25 21:53:22
Original
238 people have browsed it

C/C   Pointer Declaration: `char* p;` vs. `char *p;` – What's the Difference?

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;
Copy after login

while others prefer (b):

char *p;
Copy after login

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!

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