P
Pedro Graca
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
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