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;
When declaring a pointer type, place the asterisk next to the type name. Although you generally should not declare multiple variables on a single line, if you do, the asterisk has to be included with each variable.
An int* is a pointer to an int , so int*& must be a reference to a pointer to an int . Similarly, int** is a pointer to a pointer to an int , so int**& must be a reference to a pointer to a pointer to an int .
Dereference operator (*) Pointers are said to "point to" the variable whose address they store. An interesting property of pointers is that they can be used to access the variable they point to directly.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With