I get this error when I try to compile my code. I need to pass several arguments to start a thread and I'm having trouble with it. Here are the details I find relevant :
struct args {
int clientfd;
int serialp;
};
struct args threaded;
while(1)
{
int clientfd;
struct sockaddr_in client_addr;
int addrlen=sizeof(client_addr);
pthread_t thread1;
printf("À espera de uma nova ligação...\n");
clientfd = accept(sock, (struct sockaddr*)&client_addr, &addrlen);
threaded.clientfd = clientfd;
threaded.serialp = serialp;
pthread_create(&thread1, 0, receive_tcp, /*clientfd*/&threaded);
pthread_detach(thread1);
}
void *receive_tcp(void *args)
{
int buffer1[7];
int *buffer2;
int n, i, adress;
unsigned long int size;
int clientfd = args.clientfd;
n = read(clientfd, buffer1, sizeof(buffer1));
size = 256*buffer1[5]+buffer1[6];
buffer2 = (int *) malloc (size*sizeof(int));
n = read(clientfd, buffer2, sizeof(buffer2));
adress = buffer1[7];
add_ASCII(buffer2, size, adress, args.serialp);
}
Here's what I getting with the compiling:
Modo1.c:101:21: warning: dereferencing ‘void *’ pointer
int clientfd = args->clientfd;
^
Modo1.c:101:21: error: request for member ‘clientfd’ in something not a structure or union
Modo1.c:112:39: warning: dereferencing ‘void *’ pointer
add_ASCII(buffer2, size, adress, args->serialp);
^
Modo1.c:112:39: error: request for member ‘serialp’ in something not a structure or union
Any idea of what it might be?
void *receive_tcp(void *args)
The type of args
is void*
. You need to cast it to struct args*
before you can access the members of the struct
.
void *receive_tcp(void *temp_args)
{
int buffer1[7];
int *buffer2;
int n, i, adress;
unsigned long int size;
// Cast the argument
struct args* real_args = (struct args*)(temp_args);
// Now access the members
int clientfd = real_args->clientfd;
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.