casting structures

rex ballard rb at ccivax.UUCP
Tue Feb 25 04:46:29 AEST 1986


Well, another goofup!
First of all, the example given was poor to say the least.  It certainly
did not illustrate the problem.  Your right, I've never used it, but I
have seen it.

An example where casting was needed was this.
#define EOR 0370
char *x;
if (*x<EOR)
or
#define EOR '\370'
int *x;
if (*x<EOR)

(believe it or not, because of some #includes, both showed up in the same
project, in fact, the definitions changed mid-project)

guess what, the original compiler actually worked (gave the result desired)
with the second example and didn't work with:

#define EOR '\370'	/* compiler treated like unsigned */
char *x;
if(*x<EOR)

because of the nature of what was actually desired, the proper definition
would have been.

#define EOR (unsigned char)0370
or
#define EOR (unsigned char)'370'
unsigned char x;
if (*x<EOR)

moral: when in doubt, cast  (the next guy might figure out what you want)

Qualifier: the compiler that gave this problem accepted code which
contained so many syntax errors (missing semicolons...) that lint
died just looking at it.  (I think it was someone's kludge of bcpl or
what they thought C was before reading K&R :-)  It is a VERY OLD compiler.

Please excuse problems in the code, these are just "off the cuff" examples,
written near the end of the lunch hour.



More information about the Comp.lang.c mailing list