开发者

Why would the return type for a function be specified when it is called in C? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Need for prefixing开发者_如何学运维 a function with (void)…

Hi,

I am working with some C code and the author has used a syntax I am unfamiliar with. With the following call to a function:

(void) msleep(&gActivationCount, NULL, PUSER,
    "com_apple_dts_kext_KauthORama.RemoveListener", &oneSecond);

It would appear that the author is casting the return to void or simply stating what the function prototype probably (I say probably because I can't resolve the function to it's prototype), says for the return value. Isn't this equivalent to:

msleep(&gActivationCount, NULL, PUSER,
    com_apple_dts_kext_KauthORama.RemoveListener", &oneSecond);

Thanks,

Scott


This is an old (and extremely eyesore) trick to explicitly mark your intention to ignore the return value of a function, for lint and compilers that issue a warning. Forget you ever saw it. On modern compilers, you have to go out of your way to turn on this warning, and some (such as gcc) offer extensions to flag just the specific functions for which ignoring the return value is dangerous.


He is casting the result to void to expressly discard the value, but more often it is to get rid of a warning about an unused result (that probably should not be ignored in the first place!)

When the function already returns void, it is even more pointless to cast to (void).

Older compiler seem to have been overly eager in reporting unused values - these days, it takes for example __attribute__((warn_unused_result)) instead if you have a function that really must be checked.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜