What does (void)myVar;

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jorba101
    New Member
    • Jun 2008
    • 86

    What does (void)myVar;

    In a code I'm dealing with, I've seen following statements, at the beginning of a function:
    Code:
    static isr_t
    myIsr(vector_number nr)
    {
    	(void) nr;
    
    	// [...]
    }
    I've seen that the (void) nr; is there to prevent the compiler from complaining of not using the variable 'nr' inside the function. That's fine, but I do not understand why.

    In general, in C, what does then (void) someVar; do?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    A variable that is not used can produce a warning from the compiler to that effect.

    Often for production code, the compiler settings are altered to convert warnings to errors. Now the code won't compile. The only thing to do is use the variable somehow in a manner that doesn't do anything. Then the error goes away.

    Most likely, this is old code and it's too hard to remove the argument so the lazy developer puts this plug in there.

    In C, void means "no type". You usually see this on functions where the return type is void. That means the function returns no type and that means you cannot use the return type of the function to assign to a variable.

    Similarly, void* is a pointer to no type. That is, it is just an address. It is up to the developer to know how to use this address.

    Comment

    • jorba101
      New Member
      • Jun 2008
      • 86

      #3
      Thanks for your answer,

      But in particular I was interested in knowing why a is valid C statement simply being the name of a variable, which I didn't previously know it was possible to do (since I see no practical purpose in doing that). In my case, it is clear to me that the only reason to place that is to avoid the compiler warning. Anyway, may it have any other purpose in other contextes? (e.g. why that can be a sentence in C? Can it actually have some effect, say, using a non-optimizer compiler, and what nature of effect?)

      On the other hand, the (void) casting puzzles me more yet, considering that the statement is not meant to do anything. Which purpose or reason could be behind this cast? Maybe ensuring no code generation at the output? If so, which code would it generate without the cast?

      Thanks again,

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        This statement will have little or no effect in any compiler, optimising or not. The reason it is valid is that any valid expression is a valid statement, not an expression is something that produces a value and can be a r-value, a statement does not necessarily produce a value (a for or while or a function returning void are examples of statements that are not expressions).

        The reason for the void cast is that some compilers will issue a warning if a statement produces a value but nothing is done with that value which as wfc points out may be converted to an error in production builds. By casting to a void the output of the statement is converted to void so no warning is issued in this case.

        You are most likely to see this sort of thing in C code where the function is part of a module that dictates its input parameters (main is a example of a function like that) but the function does not need to use the parameters. Another common example I can thing of would be a scripting syntax parser. Often you might have a function to process each keyword, you would need arguments to the function to carry the rest of the command line to the keyword but some keywords may not have any additional arguments and may not need to use the parameters. The function would still need them if you wished to create a lookup table of keywords and handler functions as all the handler functions would need the same prototype.


        It should be noted that there is never any excuse for code like this in C++ this is a C only construct and if it appears in C++ code then something is wrong.

        Comment

        Working...