请教gcc编译下的几个警告该怎么去除,

zhoujiawen 2012-05-17 11:18:59
gcc编译下的几个警告怎么去除
1.报165: warning: too many arguments for format
code:
#define OutLineNum(row, line, item) {、
char buf[60], buf2[10];\
if (item) {
sprintf(buf2, "(%d)", (item));\
sprintf(buf, "%5s", buf2);}\
else{
sprintf(buf, " ", (line)+1, (item));}\
}
调用处:
int line;
static int firstItemInCurrentPage=0;
OutLineNum(line, (line+firstItemInCurrentPage), 0); //此行报warning错误,不知道该怎么解决

2.报warning: embedded '\0' in format
snprintf(strtmp, sizeof(strtmp), ">\000");//此行报警告

3.报warning: array subscript has type 'char'
char string1[30];
char string2[30]="测试%s %2d测试%-11s";
char string3[10]="测试";
char string4[10]="测试";

static unsigned char WaiShe[MAX_OUTSIDEDEVICE];
unsigned char OutDeviceTypeData[MaxOutDeviceType][DeviceNameLen];

sprintf(string1, string2, string4, WaiShe[0],OutDeviceTypeData[id]);//此行报警告
...全文
1165 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhoujiawen 2012-05-18
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
pragma warning(disable:4996)
[/Quote]

个人认为所有的警告都应当当错误去处理,只要是警告,说明代码与c99规范不符,应当修正
zhoujiawen 2012-05-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
warning: array subscript has type 'char'

OutDeviceTypeData[id]检查id是否为char
[/Quote]
哈哈,是的,一直检查sprintf的参数了,忘记id的类型了,在该类型时候把id的类型也改成char型的了
修改成unsigned char后即可
赵4老师 2012-05-17
  • 打赏
  • 举报
回复
#pragma warning(disable:4996)
liangbch 2012-05-17
  • 打赏
  • 举报
回复
编译器给出warning,说明你的代码有问题。
sprintf(buf, " ", (line)+1, (item));},
没有指定格式符,却使用了2个参数

snprintf(strtmp, sizeof(strtmp), ">\000");//此行报警告,
字符串本来就包含结束符\0,为什么你还要加上\000

sprintf(string1, string2, string4, WaiShe[0],OutDeviceTypeData[id]);//此行报警告
WaiShe[0]是 char类型,你却指定格式符 %d
OutDeviceTypeData[id] 是char型,你却指定%s

liyoubaidu 2012-05-17
  • 打赏
  • 举报
回复
pragma warning(disable:4996)
sun007700 2012-05-17
  • 打赏
  • 举报
回复
utDeviceTypeData[id]检查id是否
sun007700 2012-05-17
  • 打赏
  • 举报
回复
arning: array subscript has type
regainworld 2012-05-17
  • 打赏
  • 举报
回复
warning: array subscript has type 'char'

OutDeviceTypeData[id]检查id是否为char
zhoujiawen 2012-05-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
sprintf(string1, "%.-11s",(const char *)OutDeviceTypeData[4-1]);
试试。
[/Quote]
试了 不行
赵4老师 2012-05-17
  • 打赏
  • 举报
回复
sprintf(string1, "%.-11s",(const char *)OutDeviceTypeData[4-1]);
试试。
zhoujiawen 2012-05-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
编译器给出warning,说明你的代码有问题。
sprintf(buf, " ", (line)+1, (item));},
没有指定格式符,却使用了2个参数

snprintf(strtmp, sizeof(strtmp), ">\000");//此行报警告,
字符串本来就包含结束符\0,为什么你还要加上\000

sprintf(string1, string2, stri……
[/Quote]

谢谢,第一个问题,一直注意到define定义的宏参数,没有注意到后面那个sprintf的参数,已解决
第2个,也按照解决
第3个,参数问题出现在OutDeviceTypeData这个2维数组上,我试过,waishe的类型无影响
本意是OutDeviceTypeData存放的是一些设备名称,然后在sting1上输出界面,
如:OutDeviceTypeData[5][10] = {
"设备1","设备2""设备3""设备4".....
}
如果是第4号设备的名称,就是OutDeviceTypeData[4-1]
spritnf(string1, "%-11s", OutDeviceTypeData[4-1]);
这样使用就会报警告,尝试过对OutDeviceTypeData[4-1]进行强制类型转换(char *)、(char *)&out..
都会报警告,不知道怎么该

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧