Hacker News new | ask | show | jobs
by weltensturm 2107 days ago
By C half-assedly trying to make "* " part of the name, just so you can use "* ptr" everywhere like it is an actual variable name, you get confusions such as this.

First "foo_ptr's type is int * ", but then "The pointer has a type, too, by the way. Its type is int.".

I also refuse to agree with "An int * * 's type is int * ". :)

But I guess "the type of `* ptr` is int, the type of `ptr` is int * " would be even more confusing.

1 comments

It's a pretty bad syntax.

A demonstration of a declaration statement where the asterisk operator binds to an identifier rather than to a type:

    int my_int, *my_ptr, my_other_int;
A demonstration of a different declaration where the asterisk operator does not bind to an identifier, as an identifier isn't even needed:

    extern void my_function(int*, char*);
So the real answer is that it depends on what you're doing. Terribly clunky.

Interestingly the D programming language mostly keeps C's syntax, but handles multiple declarations differently. https://dlang.org/spec/declaration.html#declaration_syntax