More bugs

utzoo!decvax!harpo!floyd!vax135!ariel!orion!lime!burdvax!psuvax!sibley utzoo!decvax!harpo!floyd!vax135!ariel!orion!lime!burdvax!psuvax!sibley
Fri Jan 28 15:40:49 AEST 1983


	proc1 (name, value)
	short	name;
	float	value;
	{
		...
		proc2 (sizeof(name), &name, sizeof(value), &value);
		...
	}

Now we all know arguments to procedures are passed as either doubles
or longs (or pointers).  The call to "proc2" is as follows:

	proc2 (2, ptr_to_short, 8, ptr_to_double)
******************************************************

Nothing wrong with this if you read the manual carefully enough.
On p. 204 "C converts all float actual parameters to double...", but
nothing about converting char's, shorts, etc.  The reason people seem to
think that these are converted to int is just that that's usually what
happens, but it happens only because all function arguments are taken to be
expressions.  The result of an expression is an int, not a char or a
short, unless it is cast, of course.  Thus,
	proc1( (short)foo, (float)bar )
should pass foo as short, but bar as double.  The declaration of proc1
indicates that name will indeed be passed as short, but there is no way to
pass a float, so this is implicitly changed to double.

On the other hand,
	proc1( foo, bar )
will pass foo as int (probably making a mess, since proc1 expects
a short) and bar as double, even if foo is char or short in the caller.
Again, this is because the "usual conversions" for expressions are
performed on foo.

Dave Sibley
Department of Mathematics
Penn State University
psuvax!sibley



More information about the Comp.lang.c mailing list