What makes me wonder is why C ended up with such a syntax. That is, its contemporary, Pascal, has a very straightforward, unambiguous syntax.
The Clockwise/Spiral Rule of C declarations
11–20 of 72 posts
Re: The Clockwise/Spiral Rule of C declarations
#12 foo(*baz(bing,boff(*bratz)(biff)))(buff);Re: The Clockwise/Spiral Rule of C declarations
#13 str [10]*byte
which reads exactly as it is declared: "str is an array of length 10 of pointers to byte" (byte is Go equivalent of C char (mostly)).Re: The Clockwise/Spiral Rule of C declarations
#14The rule is misleading in cases like: int* arr[][10]; Spiral rule would state "arr is an array of pointers to arrays of 10 ints", where actually it would be "arr is an array of array of 10 pointers to int". Instead, when you write declarations, do it from right-to-left, e.g.: char const* argv[]; "argv is an array of pointers to constant characters" It doesn't help with reading, unfortunately.
For this reason I strongly prefer writing char const rather than const char Is there a reason to prefer the second version? It's a lot more popular in my experience.
Re: The Clockwise/Spiral Rule of C declarations
#15Re: The Clockwise/Spiral Rule of C declarations
#16What makes me wonder is why C ended up with such a syntax. That is, its contemporary, Pascal, has a very straightforward, unambiguous syntax.
The alternative would be for the type syntax to mirror the expression syntax used to construct values of the type. Functional languages tend to do this, particularly ones which prefer pattern matching over destructors.
Re: The Clockwise/Spiral Rule of C declarations
#17I used to think of the spiral rule as being a good guide, but then a commenter on HN showed me otherwise: https://news.ycombinator.com/item?id=12053206
Re: The Clockwise/Spiral Rule of C declarations
#18Way simpler: from inside out, read any subpart of the type as an expression. (Arrays have precedence over pointers, as usual.) The type that remains is that expression's type. So e.g. given the type: const char *foo[][50] the following expressions have the following types: foo -> const char *[][50] foo[0] -> const char * [50] foo[0][0] -> const char * *foo[0][0] -> const char Another example: int (*const bar)[restric…
Re: The Clockwise/Spiral Rule of C declarations
#19Contrast the first example with Golang: str [10]*byte which reads exactly as it is declared: "str is an array of length 10 of pointers to byte" (byte is Go equivalent of C char (mostly)).
On the other hand, IMHO the whole "make declarations read left-to-right" idea is misguided --- plenty of other constructs exist in programming languages which simply can't be read left-to-right, but are nested according to precedence. I mean, you might as well make 3+4*3 evaluate to 21 if you want to try making everything consistently left-to-right, but I don't really see anyone complaining about not being able to understand operator precedence...
Re: The Clockwise/Spiral Rule of C declarations
#20Contrast the first example with Golang: str [10]*byte which reads exactly as it is declared: "str is an array of length 10 of pointers to byte" (byte is Go equivalent of C char (mostly)).