C casting syntax and typedef
C declaration syntax is fairly convoluted ...
Which brings me to my question regarding casting syntax,
Simple types are easy enough, just put the type in brackets. But what about types like,
int (*2Dap) [5];
or double (*fp) (double, double);
I guessing here, the rule is to just strip away the identifier (plus the semicolon) of a normal variable declaration and put it in brackets.
So, int (*2Dap) [5];
becomes ( int (*) [5] )
and char * str;
becomes just (char *)
Is this a general rule?
As so typedef
, your new "type" would be what is your "variable" in your typedef
declaration,
eg. typedef double (*twoINoneOUT) (double, double);
"twoInoneOUT" would be your new "type".
Correct??
Just wanted to clarify. I know I'm unlikely to even need to cast these - could save me from having to typedef unnecessarily though.
I guessing here, the rule is to just strip away the identifier (plus the semicolon) of a normal variable declaration and put it in brackets.
Right. This declares a variable called x
:
int (*x)[5];
The type of the variable is int (*)[5]
, and you could cast to that type using the cast (int (*)[5])
.
As so typedef, your new "type" would be what is your "variable" in your typedef declaration,
Also correct. This declares a type alias called t
:
typedef int (*t)[5];
Syntactically, typedef
appears in the same place as a storage class specifier like static
.
This might be useful:
http://www.unixwiz.net/techtips/reading-cdecl.html
and this can be useful for what looks like total gibberish
http://cdecl.org/
精彩评论