编译报错warning: argument to ‘sizeof’ in ‘strncpy’ call is the same expression as the source;

编译报错warning: argument to ‘sizeof’ in ‘strncpy’ call is the same expression as the source; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess]

解决方案

原代码

strncpy(host, ip, sizeof(ip));

原因是使用指针的大小,而不是指向它的字符串的大小。

将strncpy换成strcpy或者strdup处理

也可以使用

 
strncpy(host, ip, sizeof(ip)-1); 
strncpy(dest, ip, strlen(ip));
strcpy(dest, ip);

参考

https://stackoverflow.com/questions/13553113/char-array-split-ip-with-strtok

https://stackoverflow.com/questions/27587090/how-to-get-rid-of-call-is-the-same-expression-as-the-source-warning-in-c

 

上一篇
下一篇