How to pass more than one value as an argument to a thread in C?

前端 未结 3 2006
梦如初夏
梦如初夏 2021-01-21 13:10

In C, how can i pass more than one argument to a thread?

Normally, I do it in a way like,

 pthread_create(&th,NULL,dosomething,(void*)connfd);


void         


        
3条回答
  •  被撕碎了的回忆
    2021-01-21 13:25

    About passing an array as an argument, of course you can do that. If you declare an array as,

    int a[3] = {1,2,2};
    

    a is like a label to the starting address of the array. Thus a represents a pointer. *a is equal to a[0] and *(a+1) is equal to a[1]. So you can pass the array to the thread as below:

    pthread_create(&th,NULL,dosomething,(void *)a);
    

    Inside the thread you can cast a back to an int * and use as an array.

提交回复
热议问题