defining unused parameters in C
Solution 1
You can cast the parameter to void
like this:
void *timer1_function(void * parameter1) {
(void) parameter1; // Suppress the warning.
// <statement>
}
Solution 2
GCC has an "attributes" facility that can be used to mark unused parameters. Use
void *timer1_function(__attribute__((unused))void *parameter1)
Solution 3
Two commonly used techniques:
1) Omit the name of the unused parameter:
void *timer1_function(void *) { ... }
2) Comment out the parameter name:
void *timer1_function(void * /*parameter1*/) { ... }
-- Chris
Solution 4
By default, GCC does not produce this warning, not even with -Wall. I think the workaround shown in other question might be needed when you have no control over the environment, but if you do, just remove the flag (-Wunused-parameter
).
johan
Updated on July 27, 2022Comments
-
johan almost 2 years
I need to use pthreat but I dont need to pass any argument to the function. Therefore, I pass NULL to the function on pthread_create. I have 7 pthreads, so gcc compiler warns me that I have 7 unsued parameters. How can I define these 7 parameters as unused in C programming? If I do not define these parameters as unused, would it cause any problem? Thank you in advance for the responses.
void *timer1_function(void * parameter1){ //<statement> } int main(int argc,char *argv[]){ int thread_check1; pthread_t timer1; thread_check1 = pthread_create( &timer1, NULL, timer1_function, NULL); if(thread_check1 !=0){ perror("thread creation failed"); exit(EXIT_FAILURE); } while(1){} return 0; }
-
Flexo about 12 yearsstackoverflow.com/a/4851173/168175 has an alternative form that works better for
volatile
apparently -
R.. GitHub STOP HELPING ICE about 12 years+1 this is the best fix. This warning is fundamentally idiotic. Whenever the address of a function is taken, GCC should turn off "unused parameter" warnings for it, because whether they're used internally or not, they're used as part of the required interface for the function. Personally I would say the same thing should apply to all external functions too...
-
Kyle Jones about 12 years@R I caught a bug in my code a few days ago thanks to this warning. I was doing a refactor of some functions and typed 0 instead of the identifier for a bitmask that came in as a function parameter. Enabling -Wextra allowed me to immediately fix a subtle bug that had been introduced days ago.
-
Étienne almost 10 years-1: I regularly find bugs thanks to this warning being activated.
-
ideasman42 over 9 yearsFor code that is many years old and modified... over time sometimes args are added and end up being unused later on, so this warning can be very useful, especially when it becomes a hassle to pass some argument to a function (lookup or if it has to be calculated), only to find its not even used.
-
Duncan Jones almost 6 yearsI get
parameter name omitted
if I do that (in C11). Pretty sure this is invalid for C (rather than C++).