snprintf()使用Warn提示:warning: format not a string literal and no format arguments
来源:互联网 发布:最新聊天软件排行 编辑:程序博客网 时间:2024/05/21 06:51
问题:
使用snprintf()完成字符串的复制操作:
#include <stdio.h>#include <stdlib.h>#include <string.h>#define ARR_SIZE(a)(sizeof((a))/sizeof((a)[0]))#define LEN_BUF5int main(){char buf[] = "0123456789";char buf1[LEN_BUF];char buf2[LEN_BUF];// char *buf2 = NULL;// Source buf greater than dest buf1 causes stack overflow// strcpy(buf1, buf);// printf("buf1 is: %s\n", buf1);// buf2 = calloc(LEN_BUF, sizeof(char));memset(buf2, 0, LEN_BUF);strncpy(buf2, buf, LEN_BUF);printf("buf2 is: %s\n", buf2);// free(buf2);memset(buf2, 0, LEN_BUF);snprintf(buf2, LEN_BUF, buf);printf("buf2 is: %s\n", buf2);return 0;}编译的时候出现错误提示:
david@ubuntu:~/wrk/tmp$ gcc -o strcpy_compare strcpy_compare.c strcpy_compare.c: In function ‘main’:strcpy_compare.c:42:2: warning: format not a string literal and no format arguments [-Wformat-security]strcpy_compare.c:42:2: warning: format not a string literal and no format arguments [-Wformat-security]david@ubuntu:~/wrk/tmp$
解决办法:
1. 根据提示,应该是format中没有占位符的参数。
snprintf的函数原型是int snprintf(char *restrict buf, size_t n, const char * restrict format, ...)
给snprintf()加上格式指示符,
snprintf(buf2, LEN_BUF, "%s", buf);重新编译,警告信息消失:
david@ubuntu:~/wrk/tmp$ gcc -o strcpy_compare strcpy_compare.c david@ubuntu:~/wrk/tmp$
问题解决。
- snprintf()使用Warn提示:warning: format not a string literal and no format arguments
- format not a string literal and no format arguments
- format not a string literal and no format arguments
- format not a string literal and no format arguments
- error: format not a string literal and no format arguments
- format not a string literal and no format arguments
- format not a string literal and no format arguments
- error: format not a string literal and no format arguments
- linux之写C代码出现warning: format not a string literal and no format arguments [-Wformat-security]
- error: format not a string literal and no format arguments [-Werror=format-security]
- :error: format not a string literal and no format arguments [-Werror=format-security]
- error: format not a string literal and no format arguments [-Werror=format-security];
- format not a string literal and no format arguments [-Werror=format-security]
- format not a string literal and no format arguments [-Werror=format-security]
- android 下 Error format not a string literal and no format arguments 解决方案
- Error format not a string literal and no format arguments解决方案
- Error format not a string literal and no format arguments解决方案
- Error format not a string literal and no format arguments解决方案
- SQL Server数据库巡检脚本
- gerrit init及邮箱配置
- ASP.NET Dropdownlist 错误 "Cannot have multiple items selected in a DropDownList."
- android下为自己定义wifimanager类
- Zend Studio 8 如何修改为UTF-8编码
- snprintf()使用Warn提示:warning: format not a string literal and no format arguments
- eclipse导入existing 文件时候乱码问题
- 字符指针变量和字符数组的区别
- Magento: 通过category name获取category信息
- xml基础知识
- iOS与Java服务器GZip压缩问题
- mysql delete和truncate区别
- XML
- 串口大师提示找不到串口的解决办法,增加注册表项目。