type casting problem

betz at decvax.UUCP betz at decvax.UUCP
Fri Jun 10 01:23:10 AEST 1983


I have been trying to write a CRC routine in C that is transportable
between machines.  In the process of doing this I have encountered the
need to fetch unsigned characters through a char pointer.  This has
revealed a difference of opinion among the various C compilers that I
have been using.

Example:

	register char *bp;
	register unsigned int csum;

	csum ^= (unsigned) *bp++;

Under VAX-11 C and under Berkeley C, the character fetched through
bp ends up being sign extended in spite of the unsigned type cast.
Under DECUS C and V7 unix C, the character is not sign extended.

Which is correct?  If the character is supposed to be sign extended,
how do I fetch an unsigned character from a char pointer?  (short of
anding the resulting character with 0xFF)  Under some compilers it
is possible to declare a variable as unsigned char, but this is not
a universal feature.  Does anyone have any suggestions on how to make
this code machine independant?

Thanks in advance,

David Betz
decvax!betz



More information about the Comp.lang.c mailing list