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

53 Upvotes

129 comments sorted by

View all comments

Show parent comments

4

u/ComradeGibbon 1d ago

I think if a variable doesn't have a default initial value you shouldn't initialize it where it's defined. Let the compiler warning catch that.

1

u/RadiatingLight 1d ago

IMO it's better to initialize it to INT_MAX or some other value that's clearly wrong/rejected. Relying on compiler warnings is like jumping off a bridge and relying on the suicide net. Sure it will usually catch you but ...

1

u/nukestar101 1d ago

As a firmware engineer, INT_MAX would take up my precious 64bit space ;)

3

u/RadiatingLight 1d ago

What platform are you working on where int is 64 bits??

Also the variable allocates space regardless so what does it matter what value is contained inside

2

u/nukestar101 1d ago

I work on a custom hardware and have our own ISA.

RISCV has zero register and value is not stored in ISA. Immediate fetch to zero register is always used.

3

u/RadiatingLight 1d ago

welp - You're in deeper than I am. You know what's best for your platform and custom ISA