array subscript type cannot be `char`?

K

Keith Thompson

Pedro Graca said:
Keith Thompson wrote: [...]
If CHAR_BIT==8, plain char may be either signed or unsigned.

If CHAR_BIT==8 and '0'==0xF0 (as it is in ECBDIC), then plain char
must be unsigned.

I realize that no matter what character set the implementation defines
or what CHAR_BIT is for the implementation, *all* characters in the
basic set defined by the Standard (digits, lowercase and uppercase
letters, <tab>, <newline>, and a few signs, ...) must be positive.
There is no guarantee for characters outside this set:

char *p = "Pedro Graça";
int i=0;
while (*p) {
charval[*p] = i++; /* possible BANG! for 'ç' */
++p;
}

And even for characters within the basic set, it's not a guarantee
that I'd want to depend on. There's no problem in the above code if I
use my own name rather than yours, but I'd still want to make sure it
works for any possible character value, probably by casting to
unsigned char. It's too easy to change the code to use a different
string literal or to get its data from somewhere else, and it's easier
to avoid the situation altogether than to document the assumption.

Even seemingly innocuous characters like '$', I think, are outside the
basic characater set; I'd be astonished to see an implementation where
'$'<0, but I'll probably run into one at the most inconvenient
possible moment.
 
J

Jordan Abel

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
subscript 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))
 
M

Michael Mair

Jordan said:
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
subscript 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()".

Cheers
Michael
 
M

Mark McIntyre

And frequently, we correct people's spelling, and fix bugs they didn't
spot, even though they didn't ask about that either.
We're discussing standard C here, not EBCDIC C.

I agree. But IMHO, and ICBW, you (and initially I) misunderstood the
thread to be about the general case when it was actually about a
specific one and thus technically offtopic. No harm done, either way.
Mark McIntyre
 
M

Michael Mair

Jordan said:
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"... ;-(

Cheers
Michael
 
J

Jordan Abel

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.
 
P

pete

Jordan said:
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
subscript 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))

I don't understand what that macro is for, or how you would use it.
If you would cast a byte sized type to unsigned char,
why wouldn't you cast a larger type to unsigned char?
 
C

CBFalconer

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

There you go - somebody finally noticed.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
J

Jordan Abel

I don't understand what that macro is for, or how you would use it.
If you would cast a byte sized type to unsigned char,
why wouldn't you cast a larger type to unsigned char?

Because the larger type is likely to be an int which is safe to use
since it will be valued either EOF or between 0 and UCHAR_MAX [e.g. the
result from getc] the issue is that a char value could be a negative
number other than EOF.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top