Declaring pointers; asterisk on the left or right of the space between the type and name?

It doesn't matter. Someone will now come along and close the question as a dupe, and someone else will show how the int* a way breaks if you declare multiple variables in the same declarations while int *a better reflects the syntactical structure of the code, and another one will show that Stroustrup prefers the int* a way and keeps the type together on the left side.

Many opinions, but no "right" way here.


It's a matter of preference, and somewhat of a holy war, just like brace style.

The style

someType* somePtr;

is emphasizing the type of the pointer variable. It is saying, essentially, "the type of somePtr is pointer-to-someType".

The style

someType *somePtr

is emphasizing the type of the pointed-to data. It is saying, essentially, "the type of data pointed to by somePtr is someType".

They both mean the same thing, but it depends on if a given programmer's mental model when creating a pointer is "focused", so to speak, on the pointed-to data or the pointer variable.

Putting it in the middle (as someType * somePtr) is trying to avoid committing to either one.


It doesn't matter, it is personal preference.

Some people like to keep the type together:

int* p;

Other people say that it should go next to the variable because of the following:

int *p, x;//declare 1 int pointer and 1 int
int *p, *x;//declare 2 int pointers.

Over time you will just overlook this and accept both variations.

Tags:

C++

C

Pointers