gpt4 book ai didi

c - 警告 : passing argument 2 of ‘getsockname’ from incompatible pointer type

转载 作者:可可西里 更新时间:2023-11-01 02:56:20 25 4
gpt4 key购买 nike

我想不通。谁能告诉我为什么会出现此错误:

警告:从不兼容的指针类型传递“getsockname”的参数 2

在下面的代码中:

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>

int main() {
int sd;
struct sockaddr_in my_addr;

bzero(&my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
my_addr.sin_port = htons(0);
my_addr.sin_addr.s_addr = INADDR_ANY;
socklen_t my_addr_size = sizeof my_addr;

if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stdout, "Cannot create socket for master socket.\n");
fprintf(stdout, "Terminating program\n\n");
exit(1);
}

if (bind(sd, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) {
fprintf (stdout, "Binding failed for master socket\n\n");
perror("bind failed");
exit (1);
}

if (getsockname(sd, &my_addr, &my_addr_size) == -1) {
perror("getsockname() failed");
return -1;
}
}

最佳答案

getsockname 的第二个参数应该是 struct sockaddr *。您传递的是 struct sockaddr_in 的地址。

关于c - 警告 : passing argument 2 of ‘getsockname’ from incompatible pointer type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20411223/

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