数组下标类型不能是`char`? [英] array subscript type cannot be `char`?

查看:372
本文介绍了数组下标类型不能是`char`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天遇到了一个奇怪的警告(对我而言)(我试图提高

UVA#10018编程挑战赛的分数)。


$ gcc -W -Wall -std = c89 -pedantic -O2 10018-clc.c -o 10018-clc

10018-clc.c:在函数main中:

10018-clc.c:22:警告:数组下标的类型为'char''


我不喜欢警告......或演员。

#include< stdio.h>


#define SIGNEDNESS

/ * #define SIGNEDNESS签名* / / *其中任何一个* /

/ * #define SIGNEDNESS unsigned * / / *定义作品 * /


static int charval [''9''+ 1];

静态无符号长x;


int main(void){

SIGNEDNESS char test [] =" 9012";

SIGNEDNESS char * p = test;


charval [''1''] = 1;

charval [''2''] = 2;

/ *同样适用于3到8 * /

charval [''9''] = 9;


x = 0; / *冗余* /

而(* p){

x * = 10;

x + = charval [* p]; / *第22行* /


/ *施放以消除警告:所有人都工作! * /

/ * x + = charval [(int)* p]; * /

/ * x + = charval [(size_t)* p]; * /

/ * x + = charval [(unsigned)* p]; * /

/ * x + = charval [(long)* p]; * /

/ * x + = charval [(wchar_t)* p]; * /

/ * x + = charval [(signed char)* p]; * /

/ * x + = charval [(unsigned char)* p]; * /


++ p;

}


printf("%lu \ n", x);

返回0;

}

这只是一个可移植性的问题吗? (我意识到警告出现

只是因为gcc的-Wall选项)


数组下标的类型是什么?

我猜想size_t,其他类型会自动升级。


我是否应该努力将所有字符串声明为已签名或

未签名? ...之前它在DS 9000上运行:)


-

如果您通过Google发布,请阅读< http:// cfaj .freeshell.org / google>

解决方案

gcc -W -Wall -std = c89 -pedantic -O2 10018-clc.c -o 10018- clc

10018-clc.c:函数`main'':

10018-clc.c:22:警告:数组下标的类型为`char''


我不喜欢警告......或演员。

#include< stdio.h>


#define SIGNEDNESS

/ * #define SIGNEDNESS签名* / / *其中任何一个* /

/ * #define SIGNEDNESS unsigned * / / *定义作品 * /


static int charval [''9''+ 1];

静态无符号长x;


int main(void){

SIGNEDNESS char test [] =" 9012";

SIGNEDNESS char * p = test;


charval [''1''] = 1;

charval [''2''] = 2;

/ *同样适用于3到8 * /

charval [''9''] = 9;


x = 0; / *冗余* /

而(* p){

x * = 10;

x + = charval [* p]; / *第22行* /


/ *施放以消除警告:所有人都工作! * /

/ * x + = charval [(int)* p]; * /

/ * x + = charval [(size_t)* p]; * /

/ * x + = charval [(unsigned)* p]; * /

/ * x + = charval [(long)* p]; * /

/ * x + = charval [(wchar_t)* p]; * /

/ * x + = charval [(signed char)* p]; * /

/ * x + = charval [(unsigned char)* p]; * /


++ p;

}


printf("%lu \ n", x);

返回0;

}

这只是一个可移植性的问题吗? (我意识到警告出现

只是因为gcc的-Wall选项)


数组下标的类型是什么?

我猜想size_t,其他类型会自动升级。


我是否应该努力将所有字符串声明为已签名或

未签名? ...之前它在DS 9000上运行:)


-

如果您通过Google发布,请阅读< http:// cfaj .freeshell.org / google>




Pedro Graca写道:

我遇到了一个奇怪的警告(对于me)今天(我试图提高UVA#10018编程挑战赛的分数)。


gcc -W -Wall -std = c89 -pedantic -O2 10018-clc.c -o 10018-clc
10018-clc.c:在函数main中:
10018-clc.c:22:警告:数组下标有输入`char''




[使用字符下标的示例程序]


技术上没有任何错误关于使用char作为数组

下标,任何整数类型都是合法的数组下标。


根据gcc文档中此警告的基本原理,

许多程序员忘记了char可以签名的事实

如果char值为负则显然会导致意外问题。

此警告使用-Wall选项启用,可以使用-Wno-char-subscripts通过

禁用。


Robert Gamble


I run into a strange warning (for me) today (I was trying to improve
the score of the UVA #10018 Programming Challenge).

$ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc
10018-clc.c: In function `main'':
10018-clc.c:22: warning: array subscript has type `char''

I don''t like warnings ... or casts.
#include <stdio.h>

#define SIGNEDNESS
/* #define SIGNEDNESS signed */ /* either of these */
/* #define SIGNEDNESS unsigned */ /* defines "works" */

static int charval[''9'' + 1];
static unsigned long x;

int main(void) {
SIGNEDNESS char test[] = "9012";
SIGNEDNESS char *p = test;

charval[''1''] = 1;
charval[''2''] = 2;
/* similarly for 3 to 8 */
charval[''9''] = 9;

x = 0; /* redundant */
while (*p) {
x *= 10;
x += charval[*p]; /* line 22 */

/* casts to get rid of warning: all of them "work"! */
/* x += charval[ (int) *p]; */
/* x += charval[ (size_t) *p]; */
/* x += charval[ (unsigned) *p]; */
/* x += charval[ (long) *p]; */
/* x += charval[ (wchar_t) *p]; */
/* x += charval[ (signed char) *p]; */
/* x += charval[ (unsigned char) *p]; */

++p;
}

printf("%lu\n", x);
return 0;
}
Is this only a question of portability? (I realize the warning appears
only because of the -Wall option to gcc)

What is the type of an array subscript?
I''d guess size_t, and other types would be promoted automatically.

Should I make an effort to declare all char stuff as either signed or
unsigned? ... before it runs on a DS 9000 :)

--
If you''re posting through Google read <http://cfaj.freeshell.org/google>

解决方案

gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc
10018-clc.c: In function `main'':
10018-clc.c:22: warning: array subscript has type `char''

I don''t like warnings ... or casts.
#include <stdio.h>

#define SIGNEDNESS
/* #define SIGNEDNESS signed */ /* either of these */
/* #define SIGNEDNESS unsigned */ /* defines "works" */

static int charval[''9'' + 1];
static unsigned long x;

int main(void) {
SIGNEDNESS char test[] = "9012";
SIGNEDNESS char *p = test;

charval[''1''] = 1;
charval[''2''] = 2;
/* similarly for 3 to 8 */
charval[''9''] = 9;

x = 0; /* redundant */
while (*p) {
x *= 10;
x += charval[*p]; /* line 22 */

/* casts to get rid of warning: all of them "work"! */
/* x += charval[ (int) *p]; */
/* x += charval[ (size_t) *p]; */
/* x += charval[ (unsigned) *p]; */
/* x += charval[ (long) *p]; */
/* x += charval[ (wchar_t) *p]; */
/* x += charval[ (signed char) *p]; */
/* x += charval[ (unsigned char) *p]; */

++p;
}

printf("%lu\n", x);
return 0;
}
Is this only a question of portability? (I realize the warning appears
only because of the -Wall option to gcc)

What is the type of an array subscript?
I''d guess size_t, and other types would be promoted automatically.

Should I make an effort to declare all char stuff as either signed or
unsigned? ... before it runs on a DS 9000 :)

--
If you''re posting through Google read <http://cfaj.freeshell.org/google>



Pedro Graca wrote:

I run into a strange warning (for me) today (I was trying to improve
the score of the UVA #10018 Programming Challenge).


gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc
10018-clc.c: In function `main'':
10018-clc.c:22: warning: array subscript has type `char''



[snip example program using char subscript]

There is technically nothing "wrong" about using char as an array
subscript, any integer type is legal as an array subscript.

According to the rationale for this warning in the gcc documentation,
many programmers forget the fact that char can be signed which could
obviously lead to unexpected problems if the char value was negative.
This warning is enabled with the -Wall option and can be disabled by
using -Wno-char-subscripts.

Robert Gamble


这篇关于数组下标类型不能是`char`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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