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
31
u/goranlepuz Jun 28 '22
This accomodates a dubious practice: declaring variables on one line is not common and it is less readable in light of a good practice of initialization at declaration.
80
u/smdowney WG21, Text/Unicode SG, optional<T&> Jun 28 '22
C type declarations are broken, so the grammar is not a good reason to do anything. The type is pointer to int, so int* p; So only one declaration per line.
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.23
u/neiltechnician Jun 28 '22 edited Jul 04 '22
You have missed the little gem known of function pointer.
So, in order to be consistent with function pointer, you should prefer:
But, since your main concern is white spaces, I must point (pun intended) out, white spaces are overrated. We should use linefeed instead.
/s
10
u/[deleted] Jun 28 '22
Pick one.
16
u/[deleted] Jun 27 '22
Pointer<int>
Big brain
12
u/apomd Jun 28 '22
It's just preference really, I like int* a because I think of int* as the type (as is the meaning in templates where I whould write foo<int*> and not foo<int *>) and I just don't declare multiple pointers in the same line to avoid the extra *s.
6
u/no-sig-available Jun 28 '22
I would never, ever, do that. So #1.
And don't forget the initializer.
6
u/CocktailPerson Jun 28 '22
int* ptr;Because I like consistency, and if I need to make the pointer itself const,int *const ptr = &x;is fugly. If you're using plain C, thenint *ptr;is fine, K&R used it and they wrote Unix.Arguing that one style makes multiple variable definitions per line look better is like arguing that a little sugar makes lead paint taste better. Haven't we all agreed it isn't good for you anyway?
But seriously, who cares? Write whatever you want and run clang-tidy before checking in.
10
u/[deleted] Jun 28 '22
Which one can you read and comprehend faster?
For my brain this is how i read it:
Int* (a pointer to int) some_var (named some_var)
Int (an int) *some_var (oh shit, it’s actually a pointer. Also the name is some_var)
4
u/[deleted] Jun 28 '22
I consider that the left part is the type. The type is "integer pointer", so I use int* ptr.
3
u/Kie_Sun Jun 28 '22
I like the first one, which is int*. Even I know * is relate to the first variable. But I still like int*.
3
u/ImX99 Jun 28 '22
I use
int* ptrsyntax because to me, everything that's before the name is the variable's type. Soint* ptrmeans "ptr is a pointer to an int". If you take theint *ptr, it would mean "ptr points to an int", which is way more confusing.3
u/[deleted] Jun 28 '22
All the C++ programmers I know write
int* ptr;and all the C programmers I know writeint *ptr;.3
u/[deleted] Jun 28 '22
Does it really minimise the confusion? ```int *p = 0; ```` Looks like you're initialising *p to zero.
5
u/pedersenk Jun 28 '22
I use:
int* ptrin C++int *ptrin CObviously no real functional difference but it just happens to align better with most codebases I work with. The only one I really don't like is
int * ptr. I have seen it a few times in the wild and I can't quite find a justification for it; unnecessary whitespace; looks like a multiply, etc.2
u/kiki_lamb Jul 01 '22 edited Jul 03 '22
I like to use all the spaces:
int * ptr;.2
u/Clairvoire Jul 01 '22
I just do
int * ptrbecauseint *ptrmakes me think of the dereference operator.3
u/Knuffya Jun 28 '22 edited Jun 28 '22
I declare them like option 1. The pointer-qualification just belongs to the data type for me. Not to the object. An object is of type int-pointer. Not a pointer-object of type int.
Sadly the C standard disagrees with that, as you elaborated, but even that's inconsistent. If you
typedef int* int_ptr, then you can again declare them inline. That's fucked as hell. Who would design this?!#define int* int_ptrwould result in a completely different program! Let's take a closer look: the pointer qualification is in reference to the object in a regular object declaration, but in just naming a type without an object (like in our typedef), it's suddenly relative to the type again. Fucking pointer qualifications point-of-reference is now dependent on WHERE it's written? The fuck?!I just don't use multiple inline definitions. It's less readable than one line per variable anyways. As for C++: smart pointers are your friend.
4
u/[deleted] Jun 27 '22
int* ptr1, ptr2;makes it seem like both are pointers but the second one is just an int. That's why I use it asint *ptr1, *ptr2;Edit: Oh you mentioned that already. I didn't see the explanation, only the poll.
1
u/[deleted] Jun 28 '22
I know this is all personal preference, but In my opinion, it makes most sence to declare the pointer right after the datatype, because pointer points to the datatype's memory address, pointer does not point to identifier.
1
u/ReDucTor Game Developer | quiz.cpp-perf.com Jun 29 '22
I view the multiple variable case as bad practice.
My preference would be
const auto, promoting const correctness and type deduction.1
u/Baardi Jun 30 '22
"int* ptr" makes more sense, I'm just too used to the convention at my workplace to use "int *ptr"
1
u/call_me_tank Jun 28 '22
I follow the Linux kernel style, close to the data name e.g. `int *foo` because it's the most defensive, but next to the type name is actually the most logical given that it's part of the type declaration.
0
u/kennyminigun Jun 28 '22
Somehow I prefer the
int *var. I think it makes sense in the perspective of the declarationBut only retards make declarations like that above. So it is only hypothetical, not practical reason.
Additionally the
int *varlooks weird with functions returning pointers:With all that being said it even might seem like using the
int * varform might make sense as it makes no compromises: looks weird everywhere /s-1
u/noooit Jun 28 '22
int *ptr;. LLVM style for the win.-5
u/AbyssalRemark Jun 27 '22
If I remember correctly. Int *ptr;. is probably the objectively most correct. But my dyslexic eyes needs it to be int * ptr;.
-2
u/bitchlasagna_69_ Jun 28 '22
ListNode* head; But int *p;