473,836 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array subscript type cannot be `char`?

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>
Mar 22 '06
51 23757
Jordan Abel schrieb:
On 2006-03-28, Michael Mair <Mi**********@i nvalid.invalid> wrote:
Jordan Abel schrieb:
On 2006-03-27, Michael Mair <Mi**********@i nvalid.invalid> wrote:
Jordan Abel schrieb:
>Note: No context quoted because I'm replying to the actual issue the
>thread brings up rather than to any particular post
>
>What all this is missing is that it's silly to warn on an array
>subscrip t of type char when you don't warn on one of type signed int.
>
>Idea -- magic safe macro for isalpha:
>
>#define ISALPHA(x) isalpha(sizeof( x)==1?(unsigned char)(x):(x))

Hmmm. Nice until x is something with sideeffects like, say,
"c = getchar()".

That's why it's uppercase. To warn you.


For functionality as basic as this, I do not trust anyone
to use it consistently correctly -- including me. I remember
a then-colleague abusing a macro with the words "Oh, it's
from XY -- he surely did something clever"... ;-(


as it turns out, i _did_ do something clever. x is evaluated only once.


You are right -- my bad.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 28 '06 #51
On 2006-03-28, Michael Mair <Mi**********@i nvalid.invalid> wrote:
Jordan Abel schrieb:
On 2006-03-28, Michael Mair <Mi**********@i nvalid.invalid> wrote:
Jordan Abel schrieb:

On 2006-03-27, Michael Mair <Mi**********@i nvalid.invalid> wrote:
>Jordan Abel schrieb:
>
>
>>Note: No context quoted because I'm replying to the actual issue the
>>thread brings up rather than to any particular post
>>
>>What all this is missing is that it's silly to warn on an array
>>subscri pt of type char when you don't warn on one of type signed int.
>>
>>Idea -- magic safe macro for isalpha:
>>
>>#define ISALPHA(x) isalpha(sizeof( x)==1?(unsigned char)(x):(x))
>
>Hmmm. Nice until x is something with sideeffects like, say,
>"c = getchar()".

That's why it's uppercase. To warn you.

For functionality as basic as this, I do not trust anyone
to use it consistently correctly -- including me. I remember
a then-colleague abusing a macro with the words "Oh, it's
from XY -- he surely did something clever"... ;-(


as it turns out, i _did_ do something clever. x is evaluated only once.


You are right -- my bad.


I didn't realize it at first either, it's easy to miss.
Mar 28 '06 #52

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.