Why does lint complain about this?

Guy Harris guy at auspex.auspex.com
Wed Apr 26 04:32:09 AEST 1989


>Why does lint say that the arg. is used inconsistently?

Because it is.  You're passing an integer constant "0" to a routine
expecting a "char *".

>I thought that zero could be assigned to any pointer type.

It can, but that's because in an assignment the compiler knows that the
thing being assigned to is a pointer, and therefore that the "0" must be
converted to a null pointer constant of the appropriate pointer type. 
Unless your C compiler supports function prototypes, and unless you use
them, the compiler does not know that the "0" matches a formal parameter
of pointer type, and therefore cannot do the conversion; you have to
explicitly tell it to do so, by casting it to the appropriate pointer
type:

	try((char *)0);

(The compiler could, perhaps, in principle know in this particular case
that the formal argument is of pointer type; however, in practice,
compilers don't do that - I'm not sure whether pre-(d)pANS C
specifications permit it to do so, but the (d)pANS explicitly specifies
that the compiler should not do so.)

If you plan to compile this code only with compilers that support
function prototypes (and know for certain that your management isn't ever
going to come in the door and ask you to port it to a C implementation
that doesn't support them), you can (and should!) use them:

	void	try(char *foo);	/* "void", assuming it returns no value */

	/*
	 * As long as we're being type-correct...
	 */
	/*ARGSUSED*/
	int
	main(int argc, char **argv)
	{
		try(0);
		return 0;
	}

	void
	try(char *foo)
	{
		...
	}

in which case the compiler *will* recognize that the formal argument is
of type "char *" and will convert the "0" to a null pointer of type
"char *".

>Shouldn't lint recognize the constant 0 and realize that it is
>compatible with (char *) ?

Since the compiler won't do so, no, "lint" shouldn't do so; the code in
the form you gave it won't work on, say, machines where a null pointer
of type "char *" and an integer with the value 0 don't have the same bit
pattern (e.g., a system where an integer is 16 bits long and a "char *'
is 32 bits long), and if "lint" didn't warn you about this it would be
remiss in its duties.



More information about the Comp.lang.c mailing list