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.
means that `*w` is an `int`, meaning `w` is a pointer to an `int`.
int *y[5]
(note that `◌[]` has higher precedence than `*◌`, so this is `*(y[5])`) means that `*y[5]` is an `int`, so `y[5]` is a pointer to an `int`, meaning `y` is an array of `int` pointers.
int (*(*kitchensink[5])(int, int))[6];
means that `(*(*kitchensink[5])(int, int))[6]` is an int, so
- `*(*kitchensink[5])(int, int)` is an array of `int`.
- `(*kitchensink[5])(int, int)` is a pointer to array of `int`.
- `kitchensink[5]` is a function pointer to a function that takes `(int, int)` and returns a pointer to an array of `int`.
- `kitchensink` is an array of function pointers to functions that take `(int, int)` and return a pointer to an array of `int`.
Call me a hater but I don't like the spiral rule and I like "declaration follows use" even less.
How do you make an std::array of a given type? Wrap the existing type in an extra layer of std::array, we all know this, it makes sense, there's no reasonable alternative. How do you make a C-array of a given type? Oh boy, "prepend the array specifier before the list of existing array specifiers" (actually it's worse because you have to find the right possibly-empty array of existing array specifiers first, just because there's a list of array specifiers somewhere in the type doesn't mean it's the one you should be prepending to).
"Declaration follows use" immediately goes out the window when faced with typedeffed types being used as the base type, or (as mentioned) generics in descendant languages of C. Instead you get "declaration builds up a type by wrapping layers around a core, use breaks down a type layer by layer starting from the outside" (so, necessarily, they mirror each other). C could have worked that way, and it would have made more sense.
"Declaration follows use" is the type level equivalent of taking off your socks before taking off your shoes because that's the order in which you put them on.
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".
I thought it was a bit of a miss that C didn't use ^ as the pointer sigil. I mean it's literally pointing. I'm guessing some early terminals didn't have that character on the keyboard.
Which follows an obvious "if modifier of a base type goes to the left of the type, then the operator that uses this modifier goes to the right in the expression". Just like "array of T/[]T" translates into "arr[index]".
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).
The best summary--and easiest to remember, IMHO--is that variables are declared as they are used.
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.)
Why would you do that? Ignoring for the moment arguments of prototypes and sizes of arrays, read C declarations the way they were designed: “char *a[<whatever>]” means that the expression *a[<whatever>] has type char; “char (*a(<whatever>))[<whatever>]” means that the expression (*a(<whatever>))[<whatever>] has type char; and so on. Then you apply the normal precedence rules for expressions, and in this case only knowing them for the prefix and postfix operators is sufficient. (Hint: all prefix operators have one precedence and all postfix ones another, and you know which one binds tighter if you know what *i++ means.)
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
Some more examples:
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.
Now you can use `bar *` as a function pointer.The entire language works like this.
The way I've learned to read it is
means that `v` is an `int`. means that `*w` is an `int`, meaning `w` is a pointer to an `int`. (note that `◌[]` has higher precedence than `*◌`, so this is `*(y[5])`) means that `*y[5]` is an `int`, so `y[5]` is a pointer to an `int`, meaning `y` is an array of `int` pointers. means that `(*(*kitchensink[5])(int, int))[6]` is an int, so- `*(*kitchensink[5])(int, int)` is an array of `int`.
- `(*kitchensink[5])(int, int)` is a pointer to array of `int`.
- `kitchensink[5]` is a function pointer to a function that takes `(int, int)` and returns a pointer to an array of `int`.
- `kitchensink` is an array of function pointers to functions that take `(int, int)` and return a pointer to an array of `int`.
> int *w;
But some misguided style guides demand the misleading `int* w;`, and then act surprised by `int* w, x`;
Call me a hater but I don't like the spiral rule and I like "declaration follows use" even less.
How do you make an std::array of a given type? Wrap the existing type in an extra layer of std::array, we all know this, it makes sense, there's no reasonable alternative. How do you make a C-array of a given type? Oh boy, "prepend the array specifier before the list of existing array specifiers" (actually it's worse because you have to find the right possibly-empty array of existing array specifiers first, just because there's a list of array specifiers somewhere in the type doesn't mean it's the one you should be prepending to).
"Declaration follows use" immediately goes out the window when faced with typedeffed types being used as the base type, or (as mentioned) generics in descendant languages of C. Instead you get "declaration builds up a type by wrapping layers around a core, use breaks down a type layer by layer starting from the outside" (so, necessarily, they mirror each other). C could have worked that way, and it would have made more sense.
"Declaration follows use" is the type level equivalent of taking off your socks before taking off your shoes because that's the order in which you put them on.
> How do you make an std::array of a given type?
std::array isn't a thing in C, so you don't.
Read the whole post genious, I certainly acknowledge that.
Just for future reference, I believe the correct spelling is genius.
I can get behind deliberately misspelling as "genious" when using it sarcastically.
Oops, my mistake!
>How do you make an std::array of a given type? Wrap the existing type in an extra layer of std::array, we all know this
huh? where is the extra layer?
>How do you make a C-array of a given type? Oh boy, "prepend the array specifier before the list of existing array specifiers"?
I assume he means multi-dimensional arrays:
But the C-style array is more readable, so I am not sure what the complaint really is about.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".
The reading of `char *p` is: The following things are `char`: `*p`.
I thought it was a bit of a miss that C didn't use ^ as the pointer sigil. I mean it's literally pointing. I'm guessing some early terminals didn't have that character on the keyboard.
Borrow from the late 1990s upstart GC languages: Pointer<Type>.
i find Zig does it fairly well. there is also no ambiguity between pointer and multiply.
Yeah, they took it from Pascal:
Which follows an obvious "if modifier of a base type goes to the left of the type, then the operator that uses this modifier goes to the right in the expression". Just like "array of T/[]T" translates into "arr[index]".> "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...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).
The best summary--and easiest to remember, IMHO--is that variables are declared as they are used.
Want to write an array of function pointers that return a pointer to an array of pointers to int? Well, that's:
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.)
Why would you do that? Ignoring for the moment arguments of prototypes and sizes of arrays, read C declarations the way they were designed: “char *a[<whatever>]” means that the expression *a[<whatever>] has type char; “char (*a(<whatever>))[<whatever>]” means that the expression (*a(<whatever>))[<whatever>] has type char; and so on. Then you apply the normal precedence rules for expressions, and in this case only knowing them for the prefix and postfix operators is sufficient. (Hint: all prefix operators have one precedence and all postfix ones another, and you know which one binds tighter if you know what *i++ means.)
This 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
on Linux.This is why I like python.
str is a duck.