【发布时间】:2012-03-04 07:10:28
【问题描述】:
我的部分代码:
int server_sockfd, client_sockfd; //socket file descriptors of client and server
// ... some code
if(pthread_create(&vlakno, NULL, handle_client, (int *) client_sockfd) != 0) {
perror("Error while creating thread.");
}
我收到 “警告:从不同大小的整数转换为指针 [-Wint-to-pointer-cast]”
我的函数原型:
void *handle_client(void *args);
我发现了这个问题:
link
第一个答案说他应该使用 intptr_t 而不是 int。
我有两个问题:
在我的情况下,int 和 intptr_t 有什么区别?
我该怎么办?
我有两个想法:
第一个:(更改文件描述符的类型)
int server_sockfd, client_sockfd; //socket file descriptors of client and server
// ... some code
if(pthread_create(&vlakno, NULL, handle_client, (intptr_t *) client_sockfd) != 0) {
perror("Error while creating thread.");
}
或第二个想法:(仅在转换函数 pthread_create 中更改类型)
intptr_t server_sockfd, client_sockfd; //socket file descriptors of client and server
// ... some code
if(pthread_create(&vlakno, NULL, handle_client, (int *) client_sockfd) != 0) {
perror("Error while creating thread.");
}
编辑:
在函数 handle_client 我想这样做:
int clientSocket;
clientSocket = (int)args;
我真的很抱歉用户 cnicar 或类似的东西.. 他不幸删除了他的答案,但没关系。
他的解决方案是使用 (void *),它首先抛出了同样的错误,但它可能是由于 eclipse 的不良行为造成的 :(
所以给他留言:
好的,谢谢它现在看起来很好...... Eclipse 仍然抛出这个警告,但是当我打开和关闭它两次后,用你的编辑很好地编辑它:) 非常感谢
【问题讨论】:
-
如何传递一个指向 FD 的指针,如
&client_sockfd? -
@KerrekSB 如果您的建议可以接受,请查看我的编辑。 (不确定我何时使用指针)
标签: c sockets pointers client-server