string - C - Passing multiple pointers as parameters to pthread_create -
i new c , these concepts of pointers extremely confusing me. trying seems simple, getting lot of compilation errors.
i want spawn new thread , pass multiple pointers parameters (seems way can use global variables in c through pointers), created struct hold these pointers, wont seem compile no matter change.
here code (i took out of code, except problem seems be)
//want store pointers in pointers, when change pointer changes "global" variable void request_handler(params *parameters) { params parameter = *parameters; int *numberofotherservers = parameter.numberofservers; int *clientsock = parameter.clientsock; char ***serverlist = parameters.serverlist; struct function **functionlist[10] = parameters.functionlist[10]; } typedef struct { int *clientsock; int *numberofservers; char **serverlist; struct function *functionlist[10]; } params; struct function { char name[20]; int parameternumer; int cando; char *otherservers[10]; }; int main(int argc, const char * argv[]) { int client_sock_desc; pthread_t handler_thread; struct function functionlist[10] = {{"",0, 0, {}}}; int =0, numberofotherservers; char *serverlist[10]; //create struct pass parameters struct params parameters; parameters.clientsock = &client_sock_desc; parameters.functionlist[10] = functionlist; parameters.numberofservers = &numberofotherservers; parameters.serverlist = serverlist; //create thread new clients request handled if(pthread_create(&handler_thread, null, request_handler, parameters) != 0) { perror("pthread_create failed"); } }
c far messiest language if ever used. task should simple, yet i'm lost because of guessing of how many * or & should put on variable call it.
you should pass address of parameters
request_handler
function through pthread_create
. local variable, not appropriate pass address.
so define pointer , allocate memory it.
struct params *parameters = malloc(sizeof(*parameters));
set members appropriately using ->
, , pass is.
update: few more errors
- move definition of
params
@ top, beforerequest_handler
function. - in main define
parameters
params *parameters
notstruct params *parameters
- use
parameters->
access members instead ofparameters.
wherever pointer.
Comments
Post a Comment