Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #163727
| From | Meredith Montgomery <mmontgomery@levado.to> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | on ``warning: array subscript has type ‘char’'' |
| Date | 2021-12-06 17:40 -0300 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <86mtldsbzf.fsf@levado.to> (permalink) |
I can't explain this.
%make warning
gcc -Wall -x c -g -std=c99 -pedantic-errors warning.c -o warning
In file included from warning.c:2:
warning.c: In function ‘getop’:
warning.c:38:27: warning: array subscript has type ‘char’ [-Wchar-subscripts]
38 | while (isdigit(s[++i] = c = getch()))
| ~~~~~~~^~~~~~~~~~~~~
I understand the warning is trying to tell me that a char is not a good
idea as an array subscript as a char could be negative. However, the
array subscript is /i/, not /c/. So I'm puzzled. Thank you!
This is essentially the reverse-polish calculator by Brian Kernighan in
chapter 4 of K&R. The relevant code is the following. (See full source
of the program warning.c below.)
int getch(void) { /* get a (possibly pushed-back) character */
if (p > 0)
return buf[--p];
else
return getchar();
}
int getop(char s[]) {
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t')
; /* skip white space */
s[1] = '\0';
if (!isdigit(c)) {
return c; /* not a digit, not an integer */
}
i = 0;
if (isdigit(c)) /* read the integer into s until its end */
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0'; /* close the string */
if (c != EOF)
ungetch(c);
return NUMBER;
}
--8<---------------cut here---------------start------------->8---
--8<---------------cut here---------------end--------------->8---
(*) Full source code
#include <stdio.h>
#include <ctype.h>
#define NUMBER '\0'
#define BUFSIZE 100
char buf[BUFSIZE]; /* a buffer for ungetch() */
int p = 0; /* the next free position in buf */
int getch(void) { /* get a (possibly pushed-back) character */
if (p > 0)
return buf[--p];
else
return getchar();
}
void ungetch(int c) {
if (p >= BUFSIZE)
printf("ungetch: too many characters; buffer full\n");
else
buf[p++] = c;
}
int getop(char s[]) {
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t')
; /* skip white space */
s[1] = '\0';
if (!isdigit(c)) {
return c; /* not a digit, not an integer */
}
i = 0;
if (isdigit(c)) /* read the integer into s until its end */
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0'; /* close the string */
if (c != EOF)
ungetch(c);
return NUMBER;
}
int main(void) {
char buf[100]; int i;
ungetch('1');
i = getop(buf); printf("i = %d, buf = %s\n", i, buf);
return 0;
}
Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread
on ``warning: array subscript has type ‘char’'' Meredith Montgomery <mmontgomery@levado.to> - 2021-12-06 17:40 -0300
Re: on ``warning: array subscript has type ‘char’'' Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-12-06 13:12 -0800
Re: on ``warning: array subscript has type ‘char’'' Meredith Montgomery <mmontgomery@levado.to> - 2021-12-07 15:11 -0300
csiph-web