The Clockwise/Spiral Rule of C declarations
1–10 of 72 posts
Re: The Clockwise/Spiral Rule of C declarations
#2Re: The Clockwise/Spiral Rule of C declarations
#3 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.
Re: The Clockwise/Spiral Rule of C declarations
#4The correct rule is "follow the C grammar". An easier to remember and also correct rule is "start at the identifier being declared; work outwards from that point, reading right until you hit a closing parenthesis, then left until you hit the corresponding open parenthesis, then resume reading right..." (this is sometimes called the "right-left rule"[2]).
The "spiral rule" dances around the truth without actually being precise enough to be useful.
[1] https://news.ycombinator.com/item?id=5079787 [2] http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html
Re: The Clockwise/Spiral Rule of C declarations
#5Re: The Clockwise/Spiral Rule of C declarations
#6The 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.
int* arr[][10];
If you index twice into arr and then dereference, you'll get an int. So arr must be an array of array of pointer to int.Re: The Clockwise/Spiral Rule of C declarations
#7The 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.
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
#8The 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.
I think the advice that helped me the most was "Declaration follows usage". int* arr[][10]; If you index twice into arr and then dereference, you'll get an int. So arr must be an array of array of pointer to int.
Re: The Clockwise/Spiral Rule of C declarations
#9 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)[restrict]
bar -> int (*const)[restrict]
*bar -> int [restrict]
(*bar)[0] -> int
One more: int (*(*f)(int))(void)
f -> int (*(*)(int))(void)
(*f) -> int (* (int))(void)
(*f)(0) -> int (* )(void)
*(*f)(0) -> int (void)
(*(*f)(0))() -> int
(In this case, the last expression is more readily written as f(0)(), since pointers to functions and functions are called using the same syntax.)Re: The Clockwise/Spiral Rule of C declarations
#10What makes me wonder is why C ended up with such a syntax. That is, its contemporary, Pascal, has a very straightforward, unambiguous syntax.