[net.lang.c] Solution to "A C Puzzle" of 12/12/83

kendall@wjh12.UUCP (Sam Kendall) (03/17/84)

I posted the following puzzle on 12/12/83:

	For what, if any, subexpressions `p' do the expressions
		*(p)
	and
		(p)[0]
	not have identical meaning?

I received a grand total of two responses, by mail.  One, from
Kenneth Almquist, was correct; the other concluded that there is no
such `p'.

I said that this wasn't a trick question, and I shouldn't have.  The
answer is that for any `p' of type function or pointer to function,
`(p)[0]' doesn't have a meaning.  It expands to `*((p) + 0)', but
pointer addition is illegal for function pointers, because pointer
addition needs to know the size of the pointed-to object, and functions
have no size.

	Sam Kendall		  {allegra,ihnp4}!wjh12!kendall
	Delft Consulting Corp.	    decvax!genrad!wjh12!kendall

chris@umcp-cs.UUCP (03/18/84)

I'm quite possibly the one who said that there is no "p" for which
"(p)[0]" and "*p" were different, and you're right, pointer to
function makes the former illegal.  I just tried giving it to
the 4.1BSD C compiler and it produces a "compiler error" no less!
I wonder if the System V PCC has the same bug (I consider it a bug
when your compiler announces an internal error for bad input).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris.umcp-cs@CSNet-Relay

eric@wucs.UUCP (03/22/84)

[Elrod of Melvinbone, and his trusty sword Linesucker...]


The varargs(3) stuff is particularly sensitive to this horseydoodoo.

Try passing a pointer to an integer function using varargs, and getting
it with a line such as

	int (*funcs)();

	...

	funcs=va_arg(ap,int (*p)());

The 4.2 BSD (Bugs Surface Daily) compiler does the Technicolor Yawn
on this one.  The fix is FM as far as I can tell:

	typedef int (*pintf)();

	pintf func;

	func=va_arg(ap,pintf);


works just fine.  As Bill the Cat would say, " GACK! ".

eric
-- 
..!ihnp4!afinitc!wucs!eric