Ignoring return values in C

前端 未结 10 1449
轻奢々
轻奢々 2020-12-08 13:39

Lately, I started using lint for static code analysis. One of the warning I get sometimes is regarding this issue. Let\'s say for instance that I\'ve got the following funct

10条回答
  •  生来不讨喜
    2020-12-08 14:09

    I personally like the "unused" warnings, but on occasion there are instances where I have to ignore them (e.g., the write() to user, or fscanf(...,"%*s\n") or strtol() where the return value is unimportant and I just want the side effect of [maybe] moving the file pointer along.)

    With gcc 4.6, it's getting quite tricky.

    • Casting to (void) no longer works.
    • Re-writing functions (especially variadic) is tedious and clumsy.
    • {ssize_t ignore; ignore=write(...);} throws up another warning (assigned-not-used).
    • write(...)+1 throws up yet another warning (computed-value-not-used).

    The only good (if ugly) way to suppress these is to convert the return value into something that the compiler agrees that you can ignore.

    E.g., (void)(write(...)+1).

    This is apparently progress. (And +0 does not work, BTW.)

提交回复
热议问题