Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Declaration - Pointer Asterisk Position

Tags:

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; 
like image 664
Feyyaz Avatar asked Apr 24 '10 10:04

Feyyaz


People also ask

Where should the asterisk go in a pointer?

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.

What does * and& indicate in pointer?

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 .

What does * In pointer mean?

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.

What data type is an asterisk in C++?

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".


1 Answers

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.

like image 164
Kate Gregory Avatar answered Oct 03 '22 17:10

Kate Gregory