error: ‘strncpy’ output truncated before terminating nul copying as many bytes from a string as its

源代码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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值