gpt4 book ai didi

c - 如何修复 "format specifies type ' char *' but the argument has type ' char * *'"错误

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

我正在设置一个客户端套接字系统,除了引用 server_response 的部分之外,所有的代码都会生成。我正在关注一个教程,但评论中似乎没有其他人有这个问题。是什么导致了这个错误,我该如何解决?

我在学习本教程的过程中遇到了这个错误,评论或视频中都没有解决这个问题。 https://www.youtube.com/watch?v=LtXEMwSG5-8

int network_socket;
network_socket = socket(AF_INET, SOCK_STREAM, 0);

//Address structure that holds access info for socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET; //specifies family or type of address, in this case an internet based one
server_address.sin_port = htons(13001); //specifies port
server_address.sin_addr.s_addr = INADDR_ANY;

//specifies socket, address point and the size of the address we are trying to connect to
//we save it as an int as if the connection is good it will return 0 if not it will be -1
int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
//checks for any error
if(connection_status == -1){
printf("There was an error connecting to the remote socket\n\n\n");
return 1;
}
//checks socket for any data size 250 chars long
char* server_response[300];
recv(network_socket, &server_response, sizeof(server_response), 0);

//prints data we get back
printf("server says:%s", server_response);
//closes socket
close(network_socket);
return 0;

最佳答案

char* server_response[300];
// ^

这里不应该有*。它应该是一个包含 300 个字符的数组。这样,下一行中的 sizeof(server_response) 将计算为 300,而 recv() 将从套接字读取最多 300 个字节。

关于c - 如何修复 "format specifies type ' char *' but the argument has type ' char * *'"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55885810/

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