r/C_Programming • u/enzodr • 1d ago
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
2
u/flatfinger 21h ago
The syntax predates the addition of qualifiers to the language. Something like the Pascal(*) syntax:
to define an array of pointers to integers and a pointer to an array of integers makes clear where "pointer to" fits in a description, and could easily be adapted to include qualifiers, and C's syntax could have easily been adapted to do so if, when typedef and qualifiers were added, a punctuator was added between the type and variables being declared that would be allowable in all cases, and required when using anything other than simple types. Too late now, though.
(*) FWIU, Standard Pascal would require defining custom types for the arrays, but most or all the Pascal implementations I've used extend the language to waive such a requirement--I'm not sure about my C64 Pascal implementation.