Clean up "unused parameter" compiler warnings?

  • Thread starter Charles Sullivan
  • Start date
C

Charles Sullivan

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
 
O

Old Wolf

Charles said:
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); )
 
J

jacob navia

Old Wolf a écrit :
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) :)
 
F

Fred Kleinschmidt

Charles Sullivan said:
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.
 
R

Robert Gamble

Charles said:
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
 
T

Thomas J. Gritzan

Charles said:
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.
 
R

Richard Heathfield

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.
 
T

Thomas J. Gritzan

Richard said:
Thomas J. Gritzan said:

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

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

Charles Sullivan

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

Regards,
Charles Sullivan
 
P

Peter Nilsson

jacob said:
Old Wolf a écrit :

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!" ;-]
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.]
 
E

Erik de Castro Lopo

Charles said:
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
 
T

Thad Smith

Old said:
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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top