gpt4 book ai didi

c - 不使用数组时会显示数组下标类型为 'char' 的警告

转载 作者:行者123 更新时间:2023-11-30 15:31:30 25 4
gpt4 key购买 nike

嗨,我在这个简单的代码中收到了这个奇怪的警告,这让我完全困惑了。我在页面中搜索,发现这是一个警告,提醒用户避免使用字符作为矩阵索引,因为它们可以签名,但显然情况并非如此。

这是代码:

#include <stdio.h>
#include <ctype.h>

int main() {

char c='t';
if (isspace(c)==0)
printf ("%c is not a space",c);

return (EXIT_SUCCESS);
}

我的问题是警告的原因是什么?这与 isspace 需要 int 作为参数有关吗?

最佳答案

a warning to alert users to avoid using chars as index of matrix because they can be signned, but obviously this is not the case

实际上是这样的……你看到的并不是编译器看到的。

is it related with the fact that isspace expects an int as argument?

是的; isspace 是一个宏(在您的编译器实现中)访问数组...查看您的 ctype.h 或要求您的编译器扩展宏(例如 gcc -E),您将看到数组访问。

要避免警告,请使用

if (!isspace((unsigned char)c))

关于c - 不使用数组时会显示数组下标类型为 'char' 的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24749112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com