Bellybutton lint

utzoo!decvax!harpo!floyd!cmcl2!philabs!sdcsvax!sdchema!donn utzoo!decvax!harpo!floyd!cmcl2!philabs!sdcsvax!sdchema!donn
Sun Apr 10 03:59:31 AEST 1983

Reference: vaxine.115

	What flag would one use with (4.1) lint to get it to tell you that

			char *string = input;

			for(; string != '\0'; string++)

	is an error?  Seems to me that a character constant should not
	be allowed to be compared to a pointer.

				     P. Tucker Withington
				     Automatix Incorporated
				     (617) 667-7900 x2044

This is a tricky one.  The problem is that in general character
constants may NOT be compared to pointers -- in fact the only things
that may legally be compared to pointers are other pointers and the
integer 0 (C Reference Manual, Sections 7.6, 7.7).  But the relational
expression in the example is in fact testing the pointer "string"
against the integer 0, since characters are promoted to integers in
relational expressions (7.7); we don't have a type clash.  You might
expect at least a warning from the PCC or lint, however.

Well, it turns out that the PCC is too clever.  Since character values
are always promoted to integer values in expressions, the PCC doesn't
bother to save type information about character constants; they are
treated as another way of expressing integers.  (It's easy to see why
this is done when you remember that multi-character character constants
are permitted by the PCC.  Notice that this is NOT in the standard.) By
the time the error is reportable (routine chkpun() in trees.c) there is
no way to tell that the user put '\0' instead of 0.  Lint just re-uses
the PCC code and thus it couldn't say anything useful about style or
programming heuristics even if it wanted to.

The only consolation is that this only happens when you are trying to
compare with '\0'; any other character value would cause a warning.

Donn Seeley  UCSD Chemistry Dept. RRCF  ucbvax!sdcsvax!sdchema!donn
             (619) 452-4016             sdamos!donn at nprdc

More information about the Comp.lang.c mailing list