char作为数组索引在gcc中发出警告 [英] char as array index gives warning in gcc

查看:84
本文介绍了char作为数组索引在gcc中发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Atmel AVR微控制器编写C代码。因为RAM空间非常有限(大约500字节),所以我尽可能使用char变量。例如,我在几十个地方使用字符作为数组索引:


char arr [3];

char x,y,z;


for(x = 0; x <3; x ++){

arr [x] = ...; // gcc在这里发出警告

//更多代码

}


我使用的gcc AVR编译器会为每个生成警告char数组索引如上所述,导致编译器输出上的警告消息混乱。通常我喜欢干净的编译,但我认为我的char索引使用既不违法也不表示错误。


人们怎么想? gcc警告在诊断可能的错误方面是否有用?我是对的,我的用法是完全合法的吗?


我可以通过插入int cast来安静gcc,但这会使代码混乱,并且对我来说是对读者是的,我知道我在做一些有问题的事情,但相信我已经分析了情况并且代码是正确的,但正如我所说,我不认为我的char指数值得怀疑。


谢谢,


Geno

I''m writing C code for an Atmel AVR micro controller. Because RAM space is extremely limited (about 500 bytes) I use char variables wherever I can. For example I use chars as array indices in dozens of places:

char arr[3];
char x,y,z;

for (x=0; x<3; x++) {
arr[x] = ...; // gcc gives warning here
// more code
}

The gcc AVR compiler I use generates a warning for each char array index as above, leading to warning message clutter on the compiler output. Normally I like clean compiles, but I think that my char index usage is neither illegal nor indicative of an error.

What do people think? Is the gcc warning in any way useful in diagnosing a possible error? Am I right that my usage is completely legal?

I might be able to quiet down gcc by putting in int casts, but that would clutter the code, and to me casts say to the reader "yes I know I''m doing something questionable, but trust me I''ve analyzed the situation and the code is correct", yet as I said I don''t think my char indices are questionable.

Thanks,

Geno

推荐答案

默认是int

default is int


Eugene Rice写道:
Eugene Rice wrote:

我正在写一个C代码Atmel AVR微控制器。因为RAM空间非常有限(大约500字节),所以我尽可能使用char变量。例如,我在几十个地方使用字符作为数组索引:


char arr [3];

char x,y,z;


for(x = 0; x <3; x ++){

arr [x] = ...; // gcc在这里发出警告

//更多代码

}


我使用的gcc AVR编译器会为每个生成警告char数组索引如上所述,导致编译器输出上的警告消息混乱。通常我喜欢干净的编译,但我认为我的char索引使用既不违法也不表示错误。


人们怎么想? gcc警告在诊断可能的错误方面是否有用?我的用法完全合法吗?
I''m writing C code for an Atmel AVR micro controller. Because RAM space is extremely limited (about 500 bytes) I use char variables wherever I can. For example I use chars as array indices in dozens of places:

char arr[3];
char x,y,z;

for (x=0; x<3; x++) {
arr[x] = ...; // gcc gives warning here
// more code
}

The gcc AVR compiler I use generates a warning for each char array index as above, leading to warning message clutter on the compiler output. Normally I like clean compiles, but I think that my char index usage is neither illegal nor indicative of an error.

What do people think? Is the gcc warning in any way useful in diagnosing a possible error? Am I right that my usage is completely legal?



是,是(在你的例子中)。

Yes, and yes (in your example).


我可以安静下来gcc通过投入int casts,但这会使代码混乱,并且我向演员说是的我知道我做了一些有问题的事情,但相信我已经分析了情况并且代码是正确的但正如我所说,我不认为我的char指数值得怀疑。
I might be able to quiet down gcc by putting in int casts, but that would clutter the code, and to me casts say to the reader "yes I know I''m doing something questionable, but trust me I''ve analyzed the situation and the code is correct", yet as I said I don''t think my char indices are questionable.



在一般情况下,char索引是有问题的,因为它们可能是签名或未签名的
,具体取决于实现。如果用一个用户提供的字符作为数组索引,那么

的值可能是负值,在大多数情况下,这意味着内存<将访问数组外的
。这显然是一个常见的

足够的问题警告。


如果你不想让GCC抱怨这个,要么使用正确的

命令行选项,用于禁用此警告(请查看文档),

或声明您的变量为signed char或unsigned char类型,

取决于您的需求。

In the general case, char indexes are problematic because they may be
either signed or unsigned, depending on the implementation. If a
user-provided character is used as an array index, it''s possible that
the value is negative, and in most cases, that would mean memory
outside of the array will be accessed. It was apparently a common
enough problem to warn about.

If you do not want GCC to complain about this, either use the correct
command-line option to disable this warning (check the documentation),
or declare your variables to be of type signed char or unsigned char,
depending on your needs.


10月21日下午1:13,Harald van D?3k < true ... @ gmail.comwrote:
On Oct 21, 1:13 pm, "Harald van D?3k" <true...@gmail.comwrote:

>

[snip]


如果您不希望GCC抱怨此问题,请使用正确的

命令行选项来禁用此警告(查看文档),

或声明您的变量的类型为signed char或unsigned char,

,具体取决于你的需求。
>
[snip]

If you do not want GCC to complain about this, either use the correct
command-line option to disable this warning (check the documentation),
or declare your variables to be of type signed char or unsigned char,
depending on your needs.



但如果你还需要签名字符而你还是想用它们索引数组

,只需将它们强制转换为unsigned char。


int main(void)

{

int v [] = {1,2,3};

char c = 0;


return v [(unsigned char )c];

}

But if you still need signed char and you still want to index array
with them, just typecast them to unsigned char.

int main(void)
{
int v[] = { 1, 2, 3 };
char c = 0;

return v[(unsigned char)c];
}


这篇关于char作为数组索引在gcc中发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆