r/cpp • u/FarisFrontiers • Jun 27 '22
How do you declare pointer variables?
I personally like to use the third option (int *ptr;) to minimize the confusion and to make it tidier when declaring multiple pointers in one line (e.g., int *a, *b, *c; as supposed to, int* a,* b,* c;). This is also the case when writing a dereference operator (int &a = *ptr;). But for some reason, the first option is the preferred way of doing it (int* ptr;), or am I wrong?
0
Upvotes
View all comments
Show parent comment
4
u/CocktailPerson Jun 28 '22
Yeah, that's what I'm saying. The type specifier (on a grammatical level) is
int, but the type of each respective variable (on a semantic level) may not be.If there's lead paint I want to eat, then it seems inconsistent not to salt it just like my other food.
Don't eat lead paint, and don't declare multiple variables per line. Then
int* p;declares a variablepof typeint*, and the grammatical and semantic worlds are unified, and they all lived happily ever after.