Ctype.h (start arrays at 1 then add 1 before looking up)

bet at ecsvax.UUCP bet at ecsvax.UUCP
Mon Mar 26 11:58:07 AEST 1984


In a lexical analyzer I wanted translate tables for values returned by
getchar() -- including EOF (-1). I wanted them FAST. So I created arrays
like this:

struct
{
	char dummy;
	char class[128];
} character=
{
	/* list of 129 values for characters, starting with EOF */
};

My reasoning was as follows: members of a structure of homogeneous composition
(no alignment problems) occupy consecutive locations in memory. C, god bless
its black-hearted soul, doesn't attempt subscript bounds checking. Finally,
character.class evaluates to a constant expression at compile time, which
C compilers can (and my reading suggests they will) simplify at compile time.
Therefore, I think I have a legal array with subscripts ranging from -1 to 127.
Anything wrong with this? Shouldn't it be faster than always using array[i+1]
(or evaluating i+1 into a temporary)? Inasmuch as I explained the trick clearly
in a comment, I am not interested in arguments like "UGLY" or "confusing".
					Bennett Todd
					...{decvax,ihnp4,akgua}!mcnc!ecsvax!bet



More information about the Comp.unix.wizards mailing list