源代码1及报错
// 源代码
strncpy((char*)&temp_buff[len], value, strlen(value));
// 报错
error: ‘strncpy’ output truncated before terminating nul copying as many bytes from a string as its length
源代码2及报错
错误:“strncpy”输出可能会被截断,从长度为254的字符串复制100字节
// 源代码
strncpy((char*)&temp_buff_name[len], value, 100);
// 报错
error: ‘strncpy’ output may be truncated copying 100 bytes from a string of length 254
C标准库说明
char *strncpy(char *dest, const char *src, size_t n) 把 src 所指向的字符串复制到 dest,最多复制 n 个字符
strncpy 是字符串复制函数,源数据 value 并不是const常量,而是一个变量,所以会报错(有警告)
修改方法
1:在编译选项里面去掉 "-Werror -Wall", 忽略告警
2:使用memcpy函数
void *memcpy(void *str1, const void *str2, size_t n) 从存储区 str2 复制 n 个字节到存储区 str1