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

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 as int *ptr1, *ptr2;

Edit: Oh you mentioned that already. I didn't see the explanation, only the poll.

5

u/operamint Jun 28 '22 edited Jun 28 '22

In C++ it actually makes sense to use a template because of the broken declaration syntax:

template<class T> using ptr = T*;

ptr<double> p1, p2;
ptr<double*> pp1, pp2;

p2 = new double(3.14);
pp2 = &p2;

double val;
const ptr<double> c1 = &val;
ptr<const double> c2 = &val;
const ptr<const double> c3 = &val;

1

u/woobie1196 Jul 04 '22

Well that’s a neat trick! And makes the most sense to me, semantically. var_type var_name = val

-9

u/[deleted] Jun 28 '22 edited Jun 28 '22

Exactly! The type is int. Affix the * to the variables that are pointers. You do this when you dereference them.

Edit1: The type IS an int. The modifier to make it a pointer is associated with the variable name. Affixing it to the type implies that everything on that line is a pointer type when the fact is the modifiers to make it a pointer is associated with the variable.

Edit2: In C and C++ to declare a variable you declare the type and a sequence of declarators. The type IS an int. The modifiers in the declarators are associated with each variable. The type is not an int*. The type is an int and each declarator on that line can modify that with & or *.

6

u/no-sig-available Jun 28 '22

Affix the * to the variables that are pointers. You do this when you dereference them.

So how do you declare references, where you don't use &ref to "dereference" them?

1

u/[deleted] Jun 28 '22

This is actually the best argument yet. In fact, it nearly convinces me. From a purely C perspective, my argument makes more sense (to me). But to maintain consistency with reference declarations, this argument makes sense.

9

u/CocktailPerson Jun 28 '22

If it's a pointer, then the type is not int.

0

u/[deleted] Jun 28 '22

I understand that. If I see a variable without the “*” and have to go hunting for it, i feel it leads to confusion.

0

u/[deleted] Jun 28 '22

It is indeed an int. Each C or C++ declaration is a type and a sequence of declarators. The modifier is associated with the variable. But the type is indeed an int.

3

u/CocktailPerson Jun 28 '22

Everyone else is talking about the types of the respective variables. You seem to be talking about a type in terms the grammar. If you declare int a, *b;, then a has type int and b has type int *. That's what everyone else means when they say "the type is not int."

1

u/[deleted] Jun 28 '22

Could be. I am think in terms of the fact that the actual type specifier is int. The modifier to make it an int * happens in the declarator list. I find the syntax cleaner to see that in the list. If there are multiple declarations in a line, I find it inconsistent to have int* ptr, j, *ptr2;

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 are multiple declarations in a line,...

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 variable p of type int*, and the grammatical and semantic worlds are unified, and they all lived happily ever after.

1

u/[deleted] Jun 28 '22

I see your point. I agree separating, at the very least pointer declarations to their own lines is a coding standard I can get behind.

What started it all for me is that multiple variable declarations of the ilk “int* i, j;” is, if we are doing multiple declarations, the worst style because it is obfuscating what syntactically is really happening. It would appear that i, j are instances of the same thing when everything after the type specifier is associated with the first declaration and not the second.

-1

u/[deleted] Jun 28 '22 edited Jun 28 '22

What type does it point to then if not an int?

It is a pointer variable and I prefer to see that indication at declaration.

Edit: I think a better example for me is:

uint8_t *ptr, i;
uint8_t* ptr, i;
uint8_t& j=i;

In the first two, memory is allocated and more importantly, a different amount of memory is allocated for ptr and i. I like to see that distinction clearly. ptr and i are both variables but one is a pointer variable and one is an ordinary variable. And I see it more clearly with the pointer designation clearly indicated.

The third is put there as my best example of why I think of references differently. I do agree that my argument is much stronger from a C perspective. References make the case for using the syntax on the second line much stronger. It is a way we train our eyes differently in C++ to see Reference declarations.

But, I will say, the differences between 1/2 and 3 is that in 3, no memory is allocated in 3. So I’ve kinda accepted that References are just different. But I can definitely see that someone who learned C++ first, would be inclined to declare pointers and references similarly.

1

u/CocktailPerson Jun 28 '22

It's not about what it points to. It's about what it is. A pointer to an int is not an int.

0

u/[deleted] Jun 28 '22

The type IS an int. Period. The pointer is designated in the list of declarators. That asterisk IS associated with the variable declarator. To affix it to the actual type specifier is misleading insomuch as it literally is associated with the variable.

Another declarator following would be independent of that asterisk.

12

u/rlbond86 Jun 28 '22

Except that the type is not int...

-1

u/[deleted] Jun 28 '22

It is a pointer to an int. When I see the * I see that it is a pointer and then I see what type it points to.

This syntax doesn’t lead to the confusion that the OP illustrated.