gpt4 book ai didi

c - 警告 : passing argument 2 of ‘inet_aton’ from incompatible pointer type. .......错误

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

我不知道这个错误是什么意思,也不知道如何修复它。

我一直在关注 Sock)et Programming Tutorials In C For Beginners | Part 2 by Eduonix在 Youtube 上,但我无法从这个人那里运行任何东西,代码来自他的教程。

是否有人可以帮助我理解此错误的含义以及如何修复它?

这里是错误:

inet_aton(address, &remote_address.sin_addr.s_addr);
^

[1007:1003 0:6758] 09:30:39 Wed May 29 [kristjan@Kundrum:pts/5 +1] ~/C_Programming
$ gcc http_client_tcp.c -o http_client_tcp
http_client_tcp.c: In function ‘main’:
http_client_tcp.c:24:24: warning: passing argument 2 of ‘inet_aton’ from incompatible pointer type [-Wincompatible-pointer-types]
inet_aton(address, &remote_address.sin_addr.s_addr);
^
In file included from http_client_tcp.c:8:0:
/usr/include/arpa/inet.h:73:12: note: expected ‘struct in_addr *’ but argument is of type ‘in_addr_t * {aka unsigned int *}’
extern int inet_aton (const char *__cp, struct in_addr *__inp) __THROW;
^~~~~~~~~

我在 Visual Studio Code 中使用 Debian Linux 9.9 stretch 和编码,但只有在 shell 中编译时,Visual Code 编辑器/调试器中才会出现错误。

代码如下:

#include <stdio.h>
#include <stdlib.h>

#include <sys/socket.h>
#include <sys/types.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <unistd.h> // for close

int main(int argc, char *argv[])
{
char *address;
address = argv[1];

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

// connect to an address
struct sockaddr_in remote_address;
remote_address.sin_family = AF_INET;
remote_address.sin_port = htons(80);
inet_aton(address, &remote_address.sin_addr.s_addr);

connect(client_socket, (struct sockaddr *) &remote_address, sizeof(remote_address));

char request[] = "GET / HTTP/1.1\r\n\r\n";
char response[4096];

send(client_socket, request, sizeof(request), 0);
recv(client_socket, &response, sizeof(response), 0);

printf("response from server: %s\n", response);
close(client_socket);

return 0;
}

最佳答案

教程不好,正确代码是

inet_aton(address, &remote_address.sin_addr);

remote_address.sin_addrin_addr类型,其定义是

struct in_addr {
unsigned long s_addr;
};

&remote_address.sin_addr.s_addr&remote_address.sin_addr 将求出相同的地址,但前者的类型错误。原始版本将在 GCC 中使用默认设置进行编译并发出警告,但它是约束冲突!

至于 Visual Studio 代码,你应该仔细看,或者用 -Werror 编译!


是时候寻找更好的教程了。

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

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