Where to put the star in C and C++ pointer notation
No. Never. <g>
But consider:
int* var1, var2;
Here, the placement of the *
is misleading, because it does not apply to var2
, which is an int
and not an int*
.
The Linux kernel coding style convention is:
int *ptr1 , *ptr2;
So I think you should adopt it as your convention.
When declaring pointer data or a function that returns a pointer type, the preferred use of
*
is adjacent to the data name or function name and not adjacent to the type name. Examples:
char *linux_banner;
unsigned long long memparse(char *ptr, char **retptr);
char *match_strdup(substring_t *s);
I believe part of the reason for this notation is so that the usage and declaration of a variable look similar.
int *var;
int x;
x = *var;
You can also think of it as saying that dereferencing var
will give you an int.