Clean up "unused parameter" compiler warnings?

Discussion in 'C Programming' started by Charles Sullivan, Sep 5, 2006.

  1. I have a number of functions, e.g.:
    int funct1( int arg1, int arg2, int arg3 );
    int funct2( int arg1, int arg2, int arg3 );
    int funct3( int arg1, int arg2, int arg3 );

    that are called via pointers in a table, with the
    same parameters regardless of the particular
    function.

    In some of the functions, one or more of the
    parameters are unused. This can result in the
    compiler warning that such and such a parameter
    is unused.

    I can silence the compiler by adding a statement
    like:
    arg2 = arg2;
    to each affected function, but that seems sort of
    clumsy.

    Is there an "approved" way of dealing with this
    which will work on all or (most all) C compilers
    and OSes?

    Thanks for your help.

    Regards,
    Charles Sullivan
     
    Charles Sullivan, Sep 5, 2006
    #1
    1. Advertising

  2. Charles Sullivan

    Old Wolf Guest

    Charles Sullivan wrote:
    > In some of the functions, one or more of the
    > parameters are unused. This can result in the
    > compiler warning that such and such a parameter
    > is unused.
    >
    > I can silence the compiler by adding a statement
    > like:
    > arg2 = arg2;
    > to each affected function, but that seems sort of
    > clumsy.


    Then you get a warning that the value of arg2 is never
    used. Ideally, turn off all these stupid warnings. But on
    compilers where that isn't possible, I do this:

    NOT_USED(arg2);

    and then in a piece of header file specific to that compiler,
    define something that will work for that compiler and not
    produce a warning. Here's some options from compilers
    I use:

    #define NOT_USED(x) ( (void)(x) )

    #define NOT_USED(x) ( *(volatile typeof(x) *)&(x) = (x); )
     
    Old Wolf, Sep 5, 2006
    #2
    1. Advertising

  3. Charles Sullivan

    jacob navia Guest

    Old Wolf a écrit :
    > Charles Sullivan wrote:
    >
    >>In some of the functions, one or more of the
    >>parameters are unused. This can result in the
    >>compiler warning that such and such a parameter
    >>is unused.
    >>
    >>I can silence the compiler by adding a statement
    >>like:
    >> arg2 = arg2;
    >>to each affected function, but that seems sort of
    >>clumsy.

    >
    >
    > Then you get a warning that the value of arg2 is never
    > used. Ideally, turn off all these stupid warnings. But on
    > compilers where that isn't possible, I do this:
    >
    > NOT_USED(arg2);
    >
    > and then in a piece of header file specific to that compiler,
    > define something that will work for that compiler and not
    > produce a warning. Here's some options from compilers
    > I use:
    >
    > #define NOT_USED(x) ( (void)(x) )
    >
    > #define NOT_USED(x) ( *(volatile typeof(x) *)&(x) = (x); )
    >


    Extensions ARE useful!

    This is a wonderful application of typeof. (Also supported
    by a certain windows compiler I know) :)
     
    jacob navia, Sep 5, 2006
    #3
  4. "Charles Sullivan" <> wrote in message
    news:p...
    >
    > I have a number of functions, e.g.:
    > int funct1( int arg1, int arg2, int arg3 );
    > int funct2( int arg1, int arg2, int arg3 );
    > int funct3( int arg1, int arg2, int arg3 );
    >
    > that are called via pointers in a table, with the
    > same parameters regardless of the particular
    > function.
    >
    > In some of the functions, one or more of the
    > parameters are unused. This can result in the
    > compiler warning that such and such a parameter
    > is unused.
    >
    > I can silence the compiler by adding a statement
    > like:
    > arg2 = arg2;
    > to each affected function, but that seems sort of
    > clumsy.
    >
    > Is there an "approved" way of dealing with this
    > which will work on all or (most all) C compilers
    > and OSes?
    >
    > Thanks for your help.
    >
    > Regards,
    > Charles Sullivan
    >


    *Some* compilers will recognize the non-standard
    /* ARGSUSED */
    comment immediately above the function definition:

    /* ARGSUSED */
    int funct1( int arg1, int arg2, int arg3 ) {
    /* Body of the function here */
    }

    Again, this is NOT standard, but you could always try it
    and see if your compiler shuts up.
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
     
    Fred Kleinschmidt, Sep 5, 2006
    #4
  5. Charles Sullivan wrote:
    > I have a number of functions, e.g.:
    > int funct1( int arg1, int arg2, int arg3 );
    > int funct2( int arg1, int arg2, int arg3 );
    > int funct3( int arg1, int arg2, int arg3 );
    >
    > that are called via pointers in a table, with the
    > same parameters regardless of the particular
    > function.
    >
    > In some of the functions, one or more of the
    > parameters are unused. This can result in the
    > compiler warning that such and such a parameter
    > is unused.
    >
    > I can silence the compiler by adding a statement
    > like:
    > arg2 = arg2;
    > to each affected function, but that seems sort of
    > clumsy.
    >
    > Is there an "approved" way of dealing with this
    > which will work on all or (most all) C compilers
    > and OSes?


    (void) arg1;

    Simple, Standard, likely to work on any compiler that complains about
    unused parameters.

    Robert Gamble
     
    Robert Gamble, Sep 5, 2006
    #5
  6. Charles Sullivan schrieb:
    > I have a number of functions, e.g.:
    > int funct1( int arg1, int arg2, int arg3 );
    > int funct2( int arg1, int arg2, int arg3 );
    > int funct3( int arg1, int arg2, int arg3 );
    >
    > that are called via pointers in a table, with the
    > same parameters regardless of the particular
    > function.
    >
    > In some of the functions, one or more of the
    > parameters are unused. This can result in the
    > compiler warning that such and such a parameter
    > is unused.


    In the function definition, I usually do (for unused arg2 and arg3):

    int funct1(int arg1, int /*arg2*/, int /*arg3*/)
    {
    /* ... */
    }

    ....or leave away the parameter name.

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
     
    Thomas J. Gritzan, Sep 5, 2006
    #6
  7. Thomas J. Gritzan said:

    > Charles Sullivan schrieb:
    >> I have a number of functions, e.g.:
    >> int funct1( int arg1, int arg2, int arg3 );
    >> int funct2( int arg1, int arg2, int arg3 );
    >> int funct3( int arg1, int arg2, int arg3 );
    >>
    >> that are called via pointers in a table, with the
    >> same parameters regardless of the particular
    >> function.
    >>
    >> In some of the functions, one or more of the
    >> parameters are unused. This can result in the
    >> compiler warning that such and such a parameter
    >> is unused.

    >
    > In the function definition, I usually do (for unused arg2 and arg3):
    >
    > int funct1(int arg1, int /*arg2*/, int /*arg3*/)
    > {
    > /* ... */
    > }
    >
    > ...or leave away the parameter name.


    ....and get a syntax error for your trouble.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Sep 5, 2006
    #7
  8. Richard Heathfield wrote:
    > Thomas J. Gritzan said:
    >> In the function definition, I usually do (for unused arg2 and arg3):
    >>
    >> int funct1(int arg1, int /*arg2*/, int /*arg3*/)
    >> {
    >> /* ... */
    >> }
    >>
    >> ...or leave away the parameter name.

    >
    > ...and get a syntax error for your trouble.


    Oops, another C++ specific, well, extension, I wasn't aware of.

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
     
    Thomas J. Gritzan, Sep 6, 2006
    #8
  9. Re: Clean up "unused parameter" compiler warnings? THANKS!

    Thanks for the responses guys, Your advice and discussion of
    the issues is as always much appreciated.

    Regards,
    Charles Sullivan
     
    Charles Sullivan, Sep 6, 2006
    #9
  10. jacob navia wrote:
    > Old Wolf a écrit :
    > > Charles Sullivan wrote:
    > > > In some of the functions, one or more of the
    > > > parameters are unused. This can result in the
    > > > compiler warning that such and such a parameter
    > > > is unused.

    > >
    > > Then you get a warning that the value of arg2 is never
    > > used. Ideally, turn off all these stupid warnings.


    Or just ignore them!!

    Compilers can generate warnings for any reason they like.
    It is futile wasting time trying to write code that doesn't issue
    any warning on any compiler.

    [I'm thinking of writing a compiler that issues the diagnostic
    "Warning: the source file compiled with no warnings. Don't think
    that just because there aren't any warnings that the code is
    necessarily correct!" ;-]

    > > But on compilers where that isn't possible, I do this:
    > >
    > > NOT_USED(arg2);
    > >
    > > and then in a piece of header file specific to that compiler,
    > > define something that will work for that compiler and not
    > > produce a warning. Here's some options from compilers
    > > I use:
    > >
    > > #define NOT_USED(x) ( (void)(x) )
    > >
    > > #define NOT_USED(x) ( *(volatile typeof(x) *)&(x) = (x); )

    >
    > Extensions ARE useful!
    >
    > This is a wonderful application of typeof.


    Wonderful?!! Removing a redundant warning?!

    I'm quite sure there are _better_ examples of typeof.

    [There are certainly more correct macros. ;-) But even after
    correcting,
    the second macro is slightly flawed in that it doesn't work properly on
    register qualified parameters.]

    --
    Peter
     
    Peter Nilsson, Sep 6, 2006
    #10
  11. Charles Sullivan wrote:
    >
    > I have a number of functions, e.g.:
    > int funct1( int arg1, int arg2, int arg3 );
    > int funct2( int arg1, int arg2, int arg3 );
    > int funct3( int arg1, int arg2, int arg3 );
    >
    > that are called via pointers in a table, with the
    > same parameters regardless of the particular
    > function.
    >
    > In some of the functions, one or more of the
    > parameters are unused. This can result in the
    > compiler warning that such and such a parameter
    > is unused.
    >
    > I can silence the compiler by adding a statement
    > like:
    > arg2 = arg2;
    > to each affected function, but that seems sort of
    > clumsy.


    If you are using GCC, you can use this:

    http://sourcefrog.net/weblog/software/languages/C/unused.html

    HTH,
    Erik
    --
    +-----------------------------------------------------------+
    Erik de Castro Lopo
    +-----------------------------------------------------------+
    http://en.wikipedia.org/wiki/Why_I_Am_Not_a_Christian
    http://en.wikipedia.org/wiki/Why_I_Am_Not_a_Muslim
    http://en.wikipedia.org/wiki/Strong_atheism
     
    Erik de Castro Lopo, Sep 6, 2006
    #11
  12. Charles Sullivan

    Thad Smith Guest

    Old Wolf wrote:
    > Charles Sullivan wrote:
    >
    >>In some of the functions, one or more of the
    >>parameters are unused. This can result in the
    >>compiler warning that such and such a parameter
    >>is unused.

    > Ideally, turn off all these stupid warnings. But on
    > compilers where that isn't possible, I do this:
    >
    > NOT_USED(arg2);
    >
    > and then in a piece of header file specific to that compiler,
    > define something that will work for that compiler and not
    > produce a warning.


    I like that approach as well.

    > Here's some options from compilers
    > I use:
    >
    > #define NOT_USED(x) ( (void)(x) )
    >
    > #define NOT_USED(x) ( *(volatile typeof(x) *)&(x) = (x); )


    I have one that responds to (if I recall correctly)

    #define NOT_USED(x) if(x);

    --
    Thad
     
    Thad Smith, Sep 11, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael B Allen

    Supressing Unused Parameter Warnings

    Michael B Allen, May 7, 2004, in forum: C Programming
    Replies:
    11
    Views:
    1,081
    CBFalconer
    May 8, 2004
  2. Replies:
    8
    Views:
    544
  3. Rui Maciel
    Replies:
    14
    Views:
    2,615
    debra h
    Oct 1, 2010
  4. Ted Sung
    Replies:
    1
    Views:
    343
    Sherm Pendley
    Aug 30, 2004
  5. mathog
    Replies:
    47
    Views:
    543
    Tim Rentsch
    Jun 12, 2014
Loading...

Share This Page