Defining unused parameters in C

I need to use pthreat, but I do not need to pass any argument to the function. Therefore, I pass NULL functions to pthread_create. I have 7 pthreads, so the gcc compiler warns me that I have 7 unauthorized parameters. How can I define these 7 parameters as unused in C programming? If I do not define these parameters as unused, will it cause any problems? Thank you in advance for your answers.

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; } 
+7
source share
5 answers

You can specify the void parameter as follows:

 void *timer1_function(void * parameter1) { (void) parameter1; // Suppress the warning. // <statement> } 
+17
source

GCC has an โ€œattributesโ€ object that can be used to mark unused parameters. Use

 void *timer1_function(__attribute__((unused))void *parameter1) 
+17
source

By default, GCC does not generate this warning, even with -Wall. I think the workaround shown in another question may be needed if you do not have control over the environment, but if you do, just remove the ( -Wunused-parameter ) flag.

+2
source

Two commonly used methods:

1) Omit the name of the unused parameter:

 void *timer1_function(void *) { ... } 

2) Comment on the parameter name:

 void *timer1_function(void * /*parameter1*/) { ... } 

- Chris

+1
source

It is perfectly fine not to use a parameter in the function body.

To avoid the compiler warning (if any in your implementation), you can do this:

 void *timer1_function(void * parameter1) { // no operation, will likely be optimized out by the compiler parameter1 = parameter1; } 
0
source

All Articles