“原型宽度不同”是什么意思?警告是什么意思?

发布于 2024-10-29 03:04:17 字数 829 浏览 3 评论 0原文

生成警告的代码,由于原型而传递了不同宽度的“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉服软 2024-11-05 03:04:17

这是由于命令行上的 -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 it isHex(char *ch) and change the call.

P.S. If this is homework, it should be tagged as such.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文