Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: size_t (http://www.velocityreviews.com/forums/t313776-re-size_t.html)

Dan Pop 06-24-2003 03:04 PM

Re: size_t
 
In <Pine.LNX.4.55L-032.0306240911210.2838@unix47.andrew.cmu.edu> "Arthur J. O'Dwyer" <ajo@andrew.cmu.edu> writes:


>On Mon, 23 Jun 2003, Peter Nilsson wrote:
>>
>> Kalle Olavi Niemitalo <kon@iki.fi> wrote...
>> > "Arthur J. O'Dwyer" writes:
>> >
>> > > int c;
>> > > while ((c=getchar()) != EOF) {
>> > > if (isalpha((unsigned char)c)) ...
>> > > }
>> > >
>> > > doesn't invoke undefined behavior for any user input,
>> > > even the ones where c is, say, a (non-EOF) negative number.

>>
>> The (unsigned char) cast fixes nothing since c is derived from
>> getchar().

>
>I.e., the cast is not needed, because fgetc() returns "an unsigned char
>converted to an int". I had not known that; thanks. However, I don't
>see any similar guarantee for the chars returned by fgets(), so just
>make an appropriate substitution, or tell me I'm wrong again. :)


fgets() doesn't return any chars, it returns a pointer.

>> > I don't think getchar() can return a non-EOF negative number,
>> > unless INT_MAX < UCHAR_MAX which AFAIK doesn't occur on any
>> > hosted implementation.

>
>You're correct. But in the case of fgets() and friends, unless I'm
>mistaken, there's nothing prohibiting the user from inputting negative
>values directly (by whatever means his system does input), if 'char' is
>a signed type on his implementation.
>
>Does it say anywhere that fgets() must act as if each input character was
>read via fgetc()?


Yes (see the quote in my other post), but this doesn't solve the problem
if plain char is signed and cannot represent the value returned by fgetc.

When calling <ctype.h> functions on characters from a string, either use a
pointer to unsigned char to access the string or use an explicit cast
to unsigned char on the char value. In theory, the former method is
more reliable (it is not perfectly clear what happens with byte values
that cannot be represented as char values), in practice both work.

The way the string was initialised simply does not matter in the context
of this discussion. Well, if it was initialised with a string literal
containing only characters from the basic source character set, no cast
is needed, because all the character values are guaranteed to be positive.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de


All times are GMT. The time now is 04:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.