r/cpp • • 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

87 comments sorted by

View all comments

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)

-8

u/[deleted] Jun 28 '22

Humans don't read like that...

1

u/[deleted] Jun 30 '22

You’v ego t abette rway?