3 ms·
A professor in college explained it in a way that works for me: C is about the type. When you say "int x" then "x" is an int. When you say "int * x", then "* x
by inlined 9y ago
A professor in college explained it in a way that works for me:
C is about the type. When you say "int x" then "x" is an int. When you say "int * x", then "* x" is an int. Humans figure out the type of x by inverting the operations in the declaration:
"int (* x[])(char * )" means that x is something you can index into and call with a string to get an int. OTOH, "int (* x)(char * )[]" is something you can call with a string and then index into to get an int.
The only gotcha is that function pointers and function names are very stubble and the compiler lets you be a little fuzzy on saying x[0]("hello, world") or (*x[0])("hello world")
(forgive spacing. Trying to avoid the formatter)
- dozzie 9y ago> OTOH, "int (* x)(char * )[]" is something you can call with a string and then index into to get an int. ...would be something you can call if it was a correct declaration. I don't think it's valid, and certainly GCC in gnu99 mode rejects it (function returning an array). The rule I heard is that you start reading the declaration from the variable name and go right to boundary (parenthesis, comma, or semicolon), then bounce left and bounce right until all tokens were seen. Then this reads as: int (*x[])(char *) "x is an array <bounce-left> of pointers <bounce-right> to a function that expects (char pointer) <bounce-right> and returns int".
- Retra 9y agoI've heard that rule too, but I've also heard it doesn't work always.