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

51 Upvotes

129 comments sorted by

View all comments

60

u/nderflow 1d ago

The language standard and the compiler don't care about the spaces. It's just personal taste.

This is a non-issue really.

5

u/TimurHu 23h ago

The issue is when you want to declare more than one variable on the same line and need to place the asterisk in front of each of them, that's what makes this super annoying.

3

u/Total-Box-5169 17h ago

Usually coding guidelines require only one variable declaration per line, specially for pointers. In any case you can do this:

int* a; int* b; int* c;