Home >Backend Development >C++ >Where Should the Asterisk Go? Understanding C Pointer Declaration Ambiguity
Pointer Definition Conundrum: The Placement of the Asterisk
In the realm of pointers, the placement of the asterisk (*) can sometimes evoke confusion. This article seeks to clarify the ambiguity by examining several pointer declaration examples.
Consider the following code snippets:
Traditionally, it was believed that the first three examples were identical, declaring a pointer named "test" to an integer. However, this is an incorrect assumption.
Instead, the placement of the asterisk is crucial. In cases 4, 5, and 6, both "test" and "test2" are pointers to integers. In contrast, in case 5, only "test" is a pointer, while "test2" is a regular integer. Conversely, case 6 is equivalent to case 5, where "test" is a pointer, and "test2" is an integer.
To avoid any further confusion, it is recommended to separate the pointer declaration from the variable declaration, as seen in the following:
int* test; int* test2;
Alternatively, you can use:
int *test, *test2;
This method ensures clarity and accuracy in pointer declarations.
The above is the detailed content of Where Should the Asterisk Go? Understanding C Pointer Declaration Ambiguity. For more information, please follow other related articles on the PHP Chinese website!