[net.unix-wizards] Problem with function returning pointer to function of type void

mullen@Nrl-Css (03/22/83)

From:  Preston Mullen <mullen@Nrl-Css>

Why does
-----
float i=97.;
float *g()	{ return &i; }
float *(*f())()	{ return g; }
main()		{ printf("%g\n",*(*f())()); }
-----
work (printing "97"), when
-----
void g()	{ printf("hi\n"); }
void (*f())()	{ return g; }
main()		{ (*f())(); }
-----
fails to compile, giving the error message
	line 2: operands of RETURN have incompatible types
?

I think the only significant difference is that "float *" has been
replaced by "void" in the definition of g and f.

P.S.  This is using the 4.1bsd c compiler.

guy (04/09/83)

	Why does
	-----
	float i=97.;
	float *g()	{ return &i; }
	float *(*f())()	{ return g; }
	main()		{ printf("%g\n",*(*f())()); }
	-----
	work (printing "97"), when
	-----
	void g()	{ printf("hi\n"); }
	void (*f())()	{ return g; }
	main()		{ (*f())(); }
	-----
	fails to compile, giving the error message
		line 2: operands of RETURN have incompatible types
	?

Because the System III C compiler (which also happens to be the 4.1BSD C
compiler, for all you trivia freaks out there), and possibly other variants
of PCC, mishandle "void".  It is a complicated mishandling, I looked into it
a long time ago, and I have since forgotten exactly what the problem is.  It
also looked complicated to fix.  It involves the way that certain type bits
are used in PCC and the fact that (if I remember correctly) certain bits don't
get turned on for functions returning "void", and PCC gets confused about the
type of the function.  That's what happens when you graft a feature (like
"void") onto something which didn't anticipate it...

					Guy Harris
					RLG Corporation
					{seismo,mcnc,we13}!rlgvax!guy