Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

C is quirky, flawed, and an enormous success -- Dennis M. Ritchie


devel / comp.lang.c / Re: on ``warning: array subscript has type ‘char’''

SubjectAuthor
* on ``warning: array subscript has type ‘char’''Meredith Montgomery
`* Re: on ``warning: array subscript has type ‘charKeith Thompson
 `- Re: on ``warning: array subscript has type ‘charMeredith Montgomery

1
on ``warning: array subscript has type ‘char’''

<86mtldsbzf.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19297&group=comp.lang.c#19297

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!ReTDBw2Ga9dYYisUeJhFXA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: on ``warning: array subscript has type ‘char’''
Date: Mon, 06 Dec 2021 17:40:52 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86mtldsbzf.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="39894"; posting-host="ReTDBw2Ga9dYYisUeJhFXA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:WvYtYRhu+Bw9gjywMONxSjdiMIM=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Mon, 6 Dec 2021 20:40 UTC

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;
}

Re: on ``warning: array subscript has type ‘char’''

<87v901bfp3.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19298&group=comp.lang.c#19298

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: on ``warning: array subscript has type ‘char
’''
Date: Mon, 06 Dec 2021 13:12:40 -0800
Organization: None to speak of
Lines: 40
Message-ID: <87v901bfp3.fsf@nosuchdomain.example.com>
References: <86mtldsbzf.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="8a44e9d9a9b2a2115bfb2aee4d38e675";
logging-data="4961"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18+ALRoyvLXeY+DKp940phy"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:j8TpB+CqyTupSegfKQ8m9Ti+ydM=
sha1:6BLnzxySspq+YQ1EjjLyrKSZfT4=
 by: Keith Thompson - Mon, 6 Dec 2021 21:12 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:
> 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!
[...]

The isdigit() function is probably implemented as a macro that uses
array indexing. (All library functions can also be implemented as
macros.) The part of the compiler that generates the warning message
doesn't know that the array indexing operator is the result of a macro
expansion, so it warns you as if you had written the indexing operator
directly.

If you replace `isdigit` by `(isdigit)` or precede the call by
#undef isdigit
it will bypass the macro and call the actual function, and you probably
won't see the warning. I suggest this as a diagnostic method, not as a
solution.

Having said that, the warning is quite useful even if it's less than
clear. The argument to isdigit() is of type int, and is required to be
either within the range of unsigned char *or* equal to EOF (which is
typically -1). Which means, annoyingly, that a char value passed to
isdigit() and friends should be cast to unsigned char. Passing a
negative char value (other than EOF) to isdigit() has undefined behavior.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Re: on ``warning: array subscript has type ‘char’''

<86czm8s2sx.fsf@levado.to>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19301&group=comp.lang.c#19301

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!XyVFIT7Rf0GBGpC6lzl4oA.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on ``warning: array subscript has type ‘char
’''
Date: Tue, 07 Dec 2021 15:11:26 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86czm8s2sx.fsf@levado.to>
References: <86mtldsbzf.fsf@levado.to>
<87v901bfp3.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="14246"; posting-host="XyVFIT7Rf0GBGpC6lzl4oA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:5EBHGr7ZvuS74F5n1hsvQfUT2hE=
 by: Meredith Montgomery - Tue, 7 Dec 2021 18:11 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> Meredith Montgomery <mmontgomery@levado.to> writes:
>> 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!
> [...]
>
> The isdigit() function is probably implemented as a macro that uses
> array indexing. (All library functions can also be implemented as
> macros.) The part of the compiler that generates the warning message
> doesn't know that the array indexing operator is the result of a macro
> expansion, so it warns you as if you had written the indexing operator
> directly.
>
> If you replace `isdigit` by `(isdigit)` or precede the call by
> #undef isdigit
> it will bypass the macro and call the actual function, and you probably
> won't see the warning. I suggest this as a diagnostic method, not as a
> solution.
>
> Having said that, the warning is quite useful even if it's less than
> clear. The argument to isdigit() is of type int, and is required to be
> either within the range of unsigned char *or* equal to EOF (which is
> typically -1). Which means, annoyingly, that a char value passed to
> isdigit() and friends should be cast to unsigned char. Passing a
> negative char value (other than EOF) to isdigit() has undefined behavior.

Lol! That's the C language we love! Order is back in the house now.
Thank you so much for the great info. It did not occur to me that
/isdigit/ could be macro. I will probably never forget this anymore.
Thank you!

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor