4 ms·
The idea that the * isn't part of the type has always felt completely wrong to me. The entire concept of an expression modifying the type doesn't provide any
by jandrese 2y ago
The idea that the * isn't part of the type has always felt completely wrong to me. The entire concept of an expression modifying the type doesn't provide any benefit that I can see. The only thing the expression should be doing is giving you the chance to assign the name to the new variable you're creating. Once you define the type, which might be a pointer or not, it should be fixed.
I get that this is a pretty fundamental disagreement between me and Knuth & Richie, but I think history has shown that this idea of splitting exactly one aspect of the type (if the variable is direct or a pointer) was a mistake. It's been the cause of a lot of bugs and the cases where you want to define both direct and indirect types on the same line basically don't exist. For clarity you should split those definitions up regardless of what the syntax allows.
There are basically no cases where you should be writing the following code even if it is technically correct:
int *pointer, direct;
- jstimpfle 2y agoI never use comma, I always define one variable per line. Much easier to edit and read. However, the way it works is certainly not inconsistent. As to your other concerns, it makes perfect sense. int WHATEVER; read as "WHATEVER is an int" and you can work backwards from there. There is no more type syntax than that. The thing is, it's much easier to look at stuff this way since otherwise you'll have to shift back and forth between 2 different syntaxes (types and expressions) all the time. I've been looking a lot for a better more algebraic syntax too. All syntaxes I've found have lost on two axes _in the common and simple cases_ - low cognitive overhead - terseness Yes, the principle of declaration follows usage is already stretched to its limit, and C++ has certainly overstretched it. But still for the bread-and-butter use cases, the "better" syntaxes seem to always lose on those 2 axes.
- zzo38computer 2y agoI only declare multiple variables in one line if it is a "simple" type, without pointers, arrays, functions, etc. Also, a code such as: int*x=y; will define the initial value of x and not of *x. For this reason, a space before the asterisk can be confusing in this way. And, for the reasons described in another comment, putting the space after the asterisk is also confusing. So, I find it clearer to omit both spaces. When I require more complicated types, I will generally use typedef (or typeof) instead of having to deal with the confusing syntax of types in C.
- shrimp_emoji 2y agoKernighan & Ritchie :p The rest of your post is perfect and objectively (subjectively) correct. While I fortunately knew that `int* x, y` means `y` is not a pointer, I always assumed it was some kind of capricious oversight of the compiler. I had no idea the rationale was "the expression `*x` declares `x` to be a pointer to the type west of it". That strikes me as so weird! XD