gpt4 book ai didi

c - "error: request for member ‘clientfd’ 在不是结构或 union 的东西中”

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:27 25 4
gpt4 key购买 nike

当我尝试编译我的代码时出现此错误。我需要传递几个参数来启动一个线程,但我遇到了麻烦。以下是我认为相关的详细信息:

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);
}

这是我通过编译得到的结果:

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

知道它可能是什么吗?

最佳答案

void *receive_tcp(void *args)

args 的类型是void*。您需要先将其转换为 struct args*,然后才能访问 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;

...

关于c - "error: request for member ‘clientfd’ 在不是结构或 union 的东西中”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29423273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com