Live data from Hacker News

The Clockwise/Spiral Rule of C declarations

c-faq.com

11–20 of 72 posts

Re: The Clockwise/Spiral Rule of C declarations

#14
post #7
post #3

The 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.

It's the difference between "declare a constant integer" and "declare an integer constant" and to me the former more accurately represents what you're doing since `const` is modifying `int`, `int` isn't modifying `const`.

Re: The Clockwise/Spiral Rule of C declarations

#15
Just don't make complex declarations in C, it's almost never useful and won't help anyone out. It'll confuse people and make your code write-only. Just put in a couple extra lines of code somewhere if you have to. It won't be the end of the world.

Re: The Clockwise/Spiral Rule of C declarations

#16
post #5

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 type syntax exactly matches the expression syntax used to destruct values of the type. It is very intuitive once you realize this.

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

#17

I 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

The real credit goes to Linus Torvalds, as I linked in that post, but I'll repeat the link again here:

https://plus.google.com/+gregkroahhartman/posts/1ZhdNwbjcYF

Re: The Clockwise/Spiral Rule of C declarations

#18

Way 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…

What happened to the const in the second example?

Re: The Clockwise/Spiral Rule of C declarations

#19

Contrast 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)).

I am not familiar with Go, and have heard many praises of its declaration syntax, but is its dereference operator postfix? That would make sense in such a case.

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

#20

Contrast 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)).

It can also be simpler if you use idiomatic modern C++ with std::array and std::function.
Post reply on HN