Clockwise/Spiral Rule (1994)
c-faq.com
Clockwise/Spiral Rule (1994)
1–10 of 59 posts
Re: Clockwise/Spiral Rule (1994)
#2The 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": https://cseweb.ucsd.edu/~gbournou/CSE131/rt_lt.rule.html).
Re: Clockwise/Spiral Rule (1994)
#3Wow, imagine if it was possible to actually use a language like that do declare the type of the variable like that? Something like
str: array [0..9] of ^Char;
or even var str [10]*uint8
Just imagine...Re: Clockwise/Spiral Rule (1994)
#4It is explained in more detail at this link: https://eigenstate.org/notes/c-decl
Re: Clockwise/Spiral Rule (1994)
#5str is a duck.
Re: Clockwise/Spiral Rule (1994)
#6Or
apt install cdecl
on Linux.Re: Clockwise/Spiral Rule (1994)
#7This comes up every so often, and while it's sort of almost true and attractive, it isn't actually correct. The 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…
Re: Clockwise/Spiral Rule (1994)
#8This comes up every so often, and while it's sort of almost true and attractive, it isn't actually correct. The 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…
Want to write an array of function pointers that return a pointer to an array of pointers to int? Well, that's:
array ... -> x[N]
... of function pointers ... -> T (*x[N])()
... that return a pointer ... -> T *(*x[N])()
... to an array ... -> T (*(*x[N])())[M]
... of pointers ... -> T *(*(*x[N])())[M]
... to int ... -> int *(*(*x[N])())[M]
It doesn't make it all that easy to read, but you can at least write the complex types pretty reliably.(The real answer is of course to just typedef every function pointer type or pointer-to-array and not worry about it anymore.)
Re: Clockwise/Spiral Rule (1994)
#9This is useful if you're far from the internet. Here's a web site that translates the gibberish to English or vice versa: https://cdecl.org/ Or apt install cdecl on Linux.
Re: Clockwise/Spiral Rule (1994)
#10Being taught this rule in undergrad really hampered my appreciation of C. As I've said in a previous comment, the real key that unlocked understanding C declarations for me is the mantra "declaration follows use." You declare a variable in C in exactly the same way you would use it: if you know how to use a variable, then you know how to read and write a declaration for it. Once I understood this elegant idea, it bec…
int v, *w, x[5], *y[5], (*z[5])(int, int);
Where v is an int, w is a pointer, x is an array, y is an array of pointers, z is an array of function pointers, etc.Similarly, typedef is also just a keyword in front of a regular declaration.
int foo[5];
typedef int foo[5];
int bar(void);
typedef int bar(void);
Now you can use `bar *` as a function pointer.The entire language works like this.