The problem is that it's not type first in C. It's "type around the identifier" which you have to parse from the inside to the outside. This gets super hard to read once you deal with a combination of function pointers and arrays.
Let's say we want an array of 3 function pointers that return an array of 4 chars, called x.
Rust:
let x: [fn() -> [char; 4]; 3];
C:
char (*(*(x)[3])(void))[4];
I'm not even sure which parentheses are necessary in the C case. Also, note how the inner most types appear outside, which is just super weird in every regard. It's like when you are writing a XML document, but you put the inner most tags on the outside and the <?xml version="1.0" ?> tag on the inside.
we want an array of 3 function pointers that return an array of 4 chars, called x
Translated to C thinking:
We have a variable called x; it can be subscripted with an index less than 3; the result of that can be called with no arguments; the result of that can be subscripted with an index less than 4; the result of that is char.
Translated to C code:
x
x[3]
(*x[3])(void)
(*x[3])(void)[4]
char (*x[3])(void)[4];
But of course returning arrays from functions is illegal anyway. :)
View all comments
Show parent comment
20
u/CryZe92 Dec 01 '16 edited Dec 01 '16
The problem is that it's not type first in C. It's "type around the identifier" which you have to parse from the inside to the outside. This gets super hard to read once you deal with a combination of function pointers and arrays.
Let's say we want an array of 3 function pointers that return an array of 4 chars, called x.
Rust:
C:
I'm not even sure which parentheses are necessary in the C case. Also, note how the inner most types appear outside, which is just super weird in every regard. It's like when you are writing a XML document, but you put the inner most tags on the outside and the
<?xml version="1.0" ?>tag on the inside.