| ▲ | The C ``Clockwise/Spiral Rule''(c-faq.com) | |||||||||||||
| 37 points by etrvic 4 hours ago | 11 comments | ||||||||||||||
| ▲ | pcfwik an hour ago | parent | next [-] | |||||||||||||
Being 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 became hard to enjoy using statically typed languages that eschew it. It is explained in more detail at this link: https://eigenstate.org/notes/c-decl | ||||||||||||||
| ||||||||||||||
| ▲ | gnramires 20 minutes ago | parent | prev | next [-] | |||||||||||||
I've written some C code recently, and it came to me perhaps the pointer syntax may not be ideal. I'm not sure what the ideal would be, but I think a different notation for usage and declaration could make it less confusion. In particular I associate '*' (used as *ptr, i.e. content that ptr points to), with content, as opposed to '&' (from &var, address of var), so again '*' means content thing points to. But in declaration, when you declare 'char *ptr', which is a pointer to a char, you clearly can't read it exactly the same way ("char with content of a pointer"? More like, the content of a pointer is char). So maybe another symbol like @ (denoting "is a pointer"), or just the keyword pointer, might make things clearer, so you'd have 'char pointer ptr' (ptr is a pointer to a char, read backwards) or simply 'char @ ptr'. The shorter '@' would be justified when you have multiple pointer e.g. when working with multidimensional arrays (which are often @@@float, something like that). Just an idea that occurred me ;) (Although I hadn't thought about pcfwik's principle that it's written as used, that makes somewhat more sense to me)* Edit: Said otherwise, in usage syntax the convention (or at least my way of thinking) may be left-to-right, "content of" or "address of", while in declaration we read right-to-left, "is an int", or "is a pointer", and it would make sense to me that the symbol for "is a pointer" is different than the symbol for "content of"/"address of". | ||||||||||||||
| ||||||||||||||
| ▲ | stephencanon 2 hours ago | parent | prev | next [-] | |||||||||||||
This 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 sometimes called the "right-left rule": https://cseweb.ucsd.edu/~gbournou/CSE131/rt_lt.rule.html). | ||||||||||||||
| ||||||||||||||
| ▲ | classified an hour ago | parent | prev | next [-] | |||||||||||||
This is useful if you're far from the internet. Here's a web site that translates the gibberish to English or vice versa: Or
on Linux. | ||||||||||||||
| ▲ | AlienRobot an hour ago | parent | prev | next [-] | |||||||||||||
This is why I like python. str is a duck. | ||||||||||||||
| ▲ | Joker_vD 2 hours ago | parent | prev [-] | |||||||||||||
> "str is an array 10 of pointers to char" Wow, imagine if it was possible to actually use a language like that do declare the type of the variable like that? Something like
or even
Just imagine... | ||||||||||||||