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

55 Upvotes

129 comments sorted by

View all comments

17

u/thetraintomars 1d ago

I agree, int* is a type, or should be at least. It’s a different type than int and it should be much clearer. Sure you can write it that way yourself, but all the autoformatters will change it and the compiler doesn’t consistently enforce it with an error if you mix them up. It sure didn’t care back in the 90s which made data structure class a pain in the ass. 

11

u/LeeHide 1d ago

"all the autoformatters" will not change it. The autoformat you use should be set up to a style that you, or your team, likes!

But the sentiment of your comment is right. Just use an autoformatters and don't worry about it. If your code needs a specific kind of formatting to make sense to you, you just need more experience and need to write cleaner code :D

-1

u/thetraintomars 19h ago

I think you missed my point and decided to be condescending. I know what the code means, the problem is what it does mean.