r/C_Programming • u/enzodr • Jul 28 '26
Does anyone else dislike pointer declaration?
For reference I am a fourth year computer engineering student. C is my preferred language.
Pointers are declared like this usually:
int *ptr_to_int;
Where the asterisk means it is a “pointer to” the specified type. The asterisk is placed immediately preceding the variable name, with no white peace.
This is what does not make sense to me. I feel that:
int* ptr_to_int;
Is far more clear. The way I see it, the asterisk modifies the type, so therefore it belongs next to the type. Putting it next to the variable name makes me think it is some kind of action or modification to the variable itself.
I think that when using * and & in code, it makes sense to apply it in front of the variable name:
int value = 3;
ptr_to_value = &value;
int copied_value = *ptr_to_value;
It is clear here that syntactically, the * represents something more like an action than a label.
Why is the convention to place the asterisk near variable name, not type? L
View all comments
Show parent comment
1
u/iOSCaleb Jul 28 '26
Yes and no. I think of a pointer as a modification of a type. Pointers are basically all the same, they only vary in what they point to. For any given type, we can declare a pointer to it without having to define a new type for that pointer. That is, if you have some type
Foo, then you can useFoo *,Foo **, etc. automatically. So if you writeFoo a, *b;, the focus is on the underlying typeFoo. I'd read that as "the types ofanand whateverbpoints to are bothFoo.That's just one way to think about it, and I mention it mainly because OP seemed to want a way to make sense of what I think is the most common practice, i.e. putting the
*with the variable name. Not everybody follows that practice. For example, although I couldn't find Google's C coding standard (if they even have one), their C++ standard requiresint* a;instead ofint *a;, and similar for references.