[英]C - Casting char array to an integer pointer?
I have the following source code and there is one line, which i cannot understand the casting is been made. 我有以下源代码,并且只有一行,我无法理解是否进行了强制转换。 Can anyone explain please? 谁能解释一下? I know the casting to an integer pointer (int *) but this is different. 我知道强制转换为整数指针(int *),但这是不同的。 I cannot understand what the final line does. 我不明白最后一行的作用。 Is it returning an integer pointer? 返回整数指针吗? or i am wrong? 还是我错了?
const unsigned char sc[] = { 0x01, 0x01, 0x01, 0x01 };
return ((int (*)(void))sc)();
(int (*)(void))sc
takes address of an array sc
and converts it to a pointer to function which returns int
. (int (*)(void))sc
获取数组sc
地址,并将其转换为指向返回int
函数的指针。
()
at the end then calls this function, and return value of this function (type int
) is again returned. ()
最后调用此函数,并再次返回此函数的返回值(类型int
)。
Weird part is that it seems as if the intention was to call function at address 0x01010101
, but it uses the address of array sc
instead, which may be an error. 奇怪的是,似乎是要在地址0x01010101
处调用函数,但它改用数组sc
的地址,这可能是错误的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.