ip_src); char c[20]="1"; strcpy(c,a); char *b=inet_-6ren">
gpt4 book ai didi

c - 指针操作

转载 作者:行者123 更新时间:2023-12-01 22:53:40 26 4
gpt4 key购买 nike

char statement[1052]="test", *a=inet_ntoa(iphdr->ip_src);
char c[20]="1";
strcpy(c,a);
char *b=inet_ntoa(iphdr->ip_dst);
snprintf(statement, 1052, "INSERT INTO tblname(ip1,ip2) VALUES ('%s','%s')",c,b);
printf("%s\n,statement);

这工作正常,但是当我尝试删除第二行和第三行时,b 的值会打印在 a 中。

最佳答案

来自http://linux.die.net/man/3/inet_ntoa

The inet_ntoa() function converts the Internet host address in, given in network byte order, to a string in IPv4 dotted-decimal notation. The string is returned in a statically allocated buffer, which subsequent calls will overwrite.

inet_ntoa 的第二次调用将覆盖第一个调用的结果。您每次都需要将值复制到您自己的存储中。

具体来说,在您的示例中,ab 最终都将指向同一个地址,该地址位于 inet_ntoa 拥有的内存中> 实现。

关于c - 指针操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26604840/

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