C++ – Pointer Asterisk Position in Type Declaration

c++pointers

in C++, the following means "allocate memory for an int pointer":

int* number;

So, the asterisk is part of the variable type; without it, that would mean "allocate memory for an int".

Then, wouldn't it make more sense if the following meant "allocate memory for two int pointers"?

int* number1, number2;

Best Answer

Stroustrup was asked this and he said (paraphrasing)

  • if you think more C-ish you will say int *a and Employee *pE (so in your head you're thinking "the content of a is an integer")
  • if you think more C++-ish you will say int* a and Employee* pE (so in your head it's "a is an integer pointer")

You can think however you like, as long as you never declare two pointers on the same line.

Works for me. I'm an Employee* pE kind of person, but I'm married to an Employee *pE kind of person - my advice would be not to get too worked up about it.