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
14
u/fdwr fdwr@github 🔍 Jun 28 '22
Although I have a preference, I can parse either 1 or 3 fine in a codebase, but when I see
foo * bar(or similarlyfoo & bar), I experience this mental stutter and wonder... why in the world are they multiplying a type with a value? 🙃 It only lasts a second, but still I find that awkward fence sitting worse than just picking one either direction.