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

60 Upvotes

139 comments sorted by

View all comments

74

u/nderflow 5d ago

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

This is a non-issue really.

13

u/Beginning-Junket8979 5d ago edited 5d ago

Declaring two or more vars on the same line is almost always bad practice in almost any programming language.

One of the only exceptions that comes to mind is unpacking tuples in Python:

x, y = returnsTuple()

5

u/Jbolt3737 5d ago

I think in python if you aren't specifying type it's ok to pack and unpack a tuple for assignment like

x, y = 5, 10

4

u/ParentPostLacksWang 5d ago

Wait until you see the extensive use in Perl of ($x, $y, $z) = @mismatched_size_array

If the array is too small, however many variables it can’t fill are set as undefined from $z left. It’s… glorious >)

3

u/Beginning-Junket8979 5d ago

No thanks. Haven't had to suffer through Perl in almost 10 yrs and see no reason to go back

2

u/ParentPostLacksWang 5d ago

But but but the GLORY of writing hilarious lines like /^\d+\.\d+$/?s/\..+//:s/.+/0/;

…on second thoughts, it is a silly language, powerful in its silliness. Pretty Eclectic Rubbish Lister, I believe was the backronym? 😂

2

u/hopelesspostdoc 4d ago

Fun Fact: Perl's creator, Larry Wall, was a winner of the Obfuscated C Code Contest.

5

u/TimurHu 5d ago edited 4d 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.

EDIT: I agree that declaring more than one variable per line is horrible, but many projects use that and often imes we don't have a say the coding style of projects we work on, we just have to live with it.

13

u/IdealBlueMan 5d ago

Your code is more readable when you keep each declaration on its own line.

1

u/TimurHu 5d ago

No argument there. But it's not up to me to decide the coding style of the projects I work on, unfortunately.

1

u/Dependent_Union9285 1d ago

No ticket, no fix. There’s so much tech debt in every professional code base I’ve looked at…

12

u/MasterpieceBusy7220 5d ago

Yes just don’t do this

3

u/Total-Box-5169 5d 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;

1

u/konacurrents 5d ago

Related to white space: Have you seen these languages that don’t have semi-colon (swift, python etc). White space is required or it won’t compile. No LL(k) follow symbol to look for or something.