“原型宽度不同”是什么意思?警告是什么意思?
生成警告的代码,由于原型而传递了不同宽度的“isHex”参数 1:
/* Checks if a character is either 0-9 or A-F */
int isHex(char ch) {
return isdigit(ch) || (ch >= 65 && ch <= 70);
}
/* Checks if a string only contains numeric characters or A-F */
int strIsHex(char * str) {
char *ch;
size_t len = strlen(str);
for(ch=str;ch<(str+len);ch++) {
if (!isHex(*ch)) return 0;
}
return 1;
}
这是什么意思,char
值不应该具有相同的宽度吗?如何将它们投射到相同的宽度以防止出现此警告?
顺便说一下,gcc 命令是: gcc.exe -std=c99 -fgnu89-inline -pedantic-errors -Wno-long-long -Wall -Wextra -Wconversion -Wshadow -Wpointer-arith -Wcast-qual - Wcast-align -Wwrite-strings -fshort-enums -gstabs -l"C:\program files\quincy\mingw\include" -o main.o -c main.c
我无法删除任何gcc 的警告选项作为作业的标记标准之一是该命令没有错误或警告。
谢谢。
The code generating the warning, passing argument 1 of 'isHex' with different width due to prototype:
/* Checks if a character is either 0-9 or A-F */
int isHex(char ch) {
return isdigit(ch) || (ch >= 65 && ch <= 70);
}
/* Checks if a string only contains numeric characters or A-F */
int strIsHex(char * str) {
char *ch;
size_t len = strlen(str);
for(ch=str;ch<(str+len);ch++) {
if (!isHex(*ch)) return 0;
}
return 1;
}
What does this mean, shouldn't the char
values be the be same width? How can I cast them to the same width to prevent this warning?
By the way, the gcc command was: gcc.exe -std=c99 -fgnu89-inline -pedantic-errors -Wno-long-long -Wall -Wextra -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -fshort-enums -gstabs -l"C:\program files\quincy\mingw\include" -o main.o -c main.c
I can't remove any of the warning options from gcc as one of the marking criteria for an assignment is that there are no errors or warnings with this command.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由于命令行上的 -Wconversion 标志造成的。它会弹出“如果原型导致的类型转换与没有原型时相同参数发生的类型转换不同”。由于默认的整型参数类型是 int,因此您无法在不触发此声明的情况下声明
isHex(char ch)
。我认为你有两个选择:声明
isHex(int ch)
并让它在调用中加宽,或者声明它isHex(char *ch)
并更改调用。PS 如果这是家庭作业,则应标记为家庭作业。
This is due to the -Wconversion flag on the command line. It pops up "if a prototype causes a type conversion that is different from what would happen to the same argument in the absence of a prototype." Since the default integral argument type is int, you can't declare
isHex(char ch)
without triggering this.You have two options, I think: declare
isHex(int ch)
and let it be widened in the call or else declare itisHex(char *ch)
and change the call.P.S. If this is homework, it should be tagged as such.