char vs. int in arg decls

Chris Torek chris at mimsy.umd.edu
Wed Sep 17 06:07:05 AEST 1986


	From: Rick Genter <rgenter at labs-b.bbn.com>

	Yes, it will work to interchange (int) and (char) as the
	types of function arguments, as long as you *never* use &,
	or *only* work on VAXen (and similar-endian machines).

It will work at other times as well.

	I tried to port [code like the below] to the Sun.

		add_char_to_line (':');		/* draw border */

		add_char_to_line (c)
		{			/* no decl for c, so default to int */
			<...>
			ins_char_in_buffer (& c, & buf);
		}

		ins_char_in_buffer (cp, bufp)
		char *cp;
		BUF *bufp;
		{
			<...>
		}

This code is wrong, and lint will say so.  It is wrong not because
':' is an int, and add_char_to_line's parameter is therefore int,
but because add_char_to_line's parameter is declared as an int and
its address (type `int *') is passed to a function expecting type
`char *'.  Were add_char_to_line declared as

	add_char_to_line(c)
		char c;
	{
		...
		ins_char_in_buffer(&c, &buf);
	}

the code would have been correct (even though the call to
add_char_to_line above passes ':', which has type `int': this is
another place where automatic promotion has odd effects).

	Dennis would have liked this guy; I said "lint" to him and
	he replied "?" :-).

Actually, I believe `?' is due to Kernighan.  (Anyone at You Know
Where care to comment?)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list