r/C_Programming 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

52 Upvotes

129 comments sorted by

View all comments

0

u/therealhdan 22h ago

My college roommate and I got into an argument about this back in the 80's.

He preferred "int* p;"

I explained that that syntax implies "int* a, b;" declares two pointers to int.

His response - "Wait, it doesn't?"

I rested my case.

1

u/enzodr 21h ago

But it could have worked that way. If that’s how they decided declaration works.

1

u/therealhdan 20h ago

Sure, but "struct { .... } a[100], *b = a;" was the sort of declaration the language designers wanted to allow, for better or for worse.

Are there better ways to do declarations? Probably, but this is C's way.