[Coco] help with C problem

James Jones jamesjones01 at mchsi.com
Sat Jan 28 19:16:53 EST 2006


Bob Devries wrote:
> To all you C gurus: I have a problem with compiling the following code:
> 
> char *
> strhcpy(s1, s2)
> char *s1;
> char *s2;
> {
>         while ((int)*s2 < 128) { /* line 8*/
>               *s1++ = *s2++;
>               }
>         *s1++ = *s2++ & 0x7F;
>         *s1 = '\0';
>         return (s1);
> }
> 
> The compiler always gives a warning  in line 8:  [Warning] comparison is 
> always true due to limited range of data type
> 
> Can someone PLEASE tell me what I'm doing wrong?

Sorry for the delay.

You're compiling on a system where just plain char is signed. (ANSI/ISO 
C has explicit signedness qualifiers that you can apply to any integral 
types, and grandfathered in the K&R notion that whether just plain 
"char" is signed can vary from compiler to compiler.) Because char is 
signed, no matter what s2 points at, *s2 will be a value between -128 
and 127 inclusive, and hence you'll never get out of the while loop.

For such a compiler, you could just as well write

     while ((*s1++ = *s2++) >= 0)
         ;
     s1[-1] &= 0x7f;
     *s1 = '\0';

though writing the loop as

     while (((*s1++ = *s2++) & 0x80) == 0)
         ;

wouldn't depend on the signedness of char. OTOH, on the 6809 it would be 
less efficient, because the first loop would become (if you were lucky 
and s1 and s2 were both in registers)

loop
     lda s1+
     sta s2+
     bge loop

because the load/store sets the condition code, and hence an explicit 
comparison needn't be emitted.

(Well, at least that's what it would do if the compiler were smart 
enough to avoid gratuitous SEX.)

	James

(It's been a long time since I looked at the docs for strhcpy(); does it 
really return the address of the byte where you put the NUL terminator?)

	James



More information about the Coco mailing list