开发者

C casting syntax and typedef

开发者 https://www.devze.com 2023-03-19 20:51 出处:网络
C declaration syntax is fairly convoluted ... Which brings me to my question regarding casting syntax,

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号