DEV Community

Discussion on: It's a pointer to a type, not a variable with an asterisk in its name

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

Ritchie intentionally designed C's declaration syntax to be "the thing on the right, if it were used in an expression, has the type of the thing on the left." So int *x means *x when used in an expression has the type int — therefore, by implication, x must be a pointer to int.

C is what it is. No amount of ranting will change it. Many before you have ranted about this same thing using the same arguments, so you're not bringing anything new to the table. If you're going to read other people's C code, you just have to get used to it. If if bothers you so much, you're free not to use C at all.

Incidentally, it's trivial to make the counter argument: the * is not part of the type (no matter how badly you might want it to be), so don't make it look like it is.

Collapse
 
mellen profile image
Matt Ellen

If that were the case then int * x would be a compiler error. Seems like Ritchie just made a mistake and forgot to point out that int* is a type, so everyone just kept making the same mistake.

Collapse
 
pauljlucas profile image
Paul J. Lucas

No, it wouldn't because whitespace largely insignificant. In your example, the "thing on the right" is still * x. The space doesn't matter.

What's actually more annoying about C is that when you declare something like int f(int a[4]) that it's highly deceptive since C doesn't have array parameters.