Does anyone know why Stroustrup's style is the placement of pointers as follows? Specifically, what Stroustrup has provided for guidance about this matter?
int* p;
vs
int *p;
because declaring multiple variables would require the asterisk next to each variable name. Which would result in:
int* p, *x;
vs
int *p, *x;
In K&R C book, they explain that the asterisk/pointer is used as a mnemonic to aid in understanding. I find it odd that the pointer/asterisk is tied to the type, vs the variable as the second of each example shows. Interested if there is some background to why the first style is chosen.
Hoping for some quote from Stroustrup in the reasoning for this.
I'm adding in K&R C 2nd Edition grammar Page 235 where the asterisk (pointer) is tied to the declarator, which is an identifier.
ANSWER
In this article from Stroustrup on coding style. He explains that both are valid and it depends on programmer preference.
I disagree that this is an opinion based question. Stroustrup's article clearly answers the question without opinion.
Best Answer
C++ emphasis heavily on types and when it comes to pointers declaration, to avoid any sort of confusion, Bjarne suggested -
Stick to one pointer per declaration
.From Bjarne Stroustrup's C++ Style and Technique FAQ [emphasis added]: