C Wizards puzzle #1 -- solution and followup

utzoo!decvax!ittvax!swatt utzoo!decvax!ittvax!swatt
Mon Nov 15 15:09:17 AEST 1982


Look people, I meant it as a JOKE! you weren't supposed to send me any
answers!  Now I have to post this silly followup article.

	From decvax!cwruecmp!boenke Fri Nov 12 00:48:36 1982
	Date: Fri Nov 12 00:27:21 1982
	Subject: C wizard's puzzle #1

	<stuff> is:	return(*((*((*((int *((*((*)()))()))a))()))()));
	which may have some extra ('s and )'s, but should work.
				Mark Boenke
				decvax!cwruecmp!boenke


	From decvax!harpo!rdb!jes Fri Nov 12 14:44:19 1982
	Date: Fri Nov 12 12:55:16 1982
	Subject: re-bugger


	It took me the thirty minutes, and I had to look in the book,
	and try several mistakes first!

			-- Jonathan Shopiro

	    int *
	    func (a)
	    int a;
	    {
		    return (*((*(int *(*((*)()))())a)()))();
	    }

	From decvax!pur-ee!ecn-pa.bruner Fri Nov 12 21:36:28 1982
	Date: Fri Nov 12 12:41:36 1982
	Subject: C puzzle #1

	My entry is:

		func(a)
		int a;
		{
			return( *(*(*(int *(*(*)())())a)())() );
		}

	--John Bruner
	  Purdue/EE



	From decvax!teklabs!ucbvax!moore at ucbcad Mon Nov 15 00:41:55 1982
	Date: Sun Nov 14 12:39:22 1982
	In Real Life: Peter Moore - EECS CAD Group, UC Berkeley


	/*
	 *  Here it is, I think.  Included is a program to actually
	 *  set a and call func.
	 *
	 *	Peter Moore
	 *
	 *	...!ucbvax!ucbcad!moore (USENET)
	 *	ucbcad.moore at berkeley   (ARPANET)
	 */

	func (a)
	int a;
	{
	    return *(*(*((int *(*(*)())()) a))())();
	}

	int *y()
	{
	   static int b; 

	   b = 12;
	   return(&b);
	}

	int *(*x())()
	{
	    return(y);
	}

	main()
	{
	    printf("%d\n", func( (int) x));
	}


	From decvax!watmath!watcgl!dmmartindale Mon Nov 15 05:46:12 1982
	Date: Sat Nov 13 22:43:13 1982
	Subject: <stuff>

	Probably the shortest expression which does what you ask is

		*(*(*(int*(*(*)())())a)())();

			< ... >

		Dave Martindale


	From decvax!harpo!npoiv!alice!rabbit!ss Mon Nov 15 13:03:11 1982
	Date: Mon Nov 15 11:01:21 1982

	You are right!!
	NO SANE PERSON WOULD WANT TO DO IT ANYWAY.
	Sharad Singhal.

Sharad (above) was the only person to submit the correct answer.

Normally, I wouldn't bother with a second article on this, but the
"contest" did produce the following:

	From decvax!teklabs!tekmdp!stevenm Mon Nov 15 00:41:00 1982
	Date: Sat Nov 13 20:13:46 1982
	Subject: Re: C wizards's puzzle #1


	Hello there, Alan. And how are things at Terribly-Huge
	Multinational, Inc.?  Things here at Tek are boring as usual.
	Well, that's not quite true, but it's raining. I thought that I
	would take the oppurtunity of your little puzzle to show off
	someone else's work - Graham's, in fact - on the assumption
	that he would be too modest to show it off himself. I think it
	is unbearably cute, and it solved your little puzzle in all of
	about a microsecond.

	First, the answer:

	Q: Convert 'int a' into type 'pointer to function returning
	   pointer to function returning pointer to int', with no
	   typdefs, etc.

	A:	func(a) {
			foo = (int *(*(*)())())a
		}

	Now, the method of solution:
	 
	----------------
	$ /a/grahamr/src/cdecl/cdecl
	cast a as pointer to function returning pointer to function \
	returning pointer to int
	(int *(*(*)())())a
	^D
	$ sss
	-----------------

	[ 'sss' is a much less interesting utility which saves the screen from
	my terminal.]

	Isn't that cute?

	Take another example:

		$ /a/grahamr/src/cdecl/cdecl
		cast b as array of pointer to function returning pointer\
		to function returning int

		(int (*(*[])())())b

		declare fp as array of pointer to function returning pointer\
		to function returning pointer to int

		int *(*(*fp[])())();

	Cute, no? Perhaps you can talk Graham into posting the thing to the net.

	mcg

	[sorry, grahamr, for spoiling this for you - I don't think you read
	net.lang.c, do you?]

	From decvax!teklabs!tekmdp!grahamr Mon Nov 15 00:43:21 1982
	Date: Sun Nov 14 17:23:44 1982
	Subject: cdecl

	Indeed, I have the tool Steve mentioned.  Indeed I do not read
	net.lang.c.  Do you think I should?  Cdecl is not too robust.
	For example, it doesn't know the PCC problem about "int(f)(){}"
	not being a recognizable function declaration.  (It recognizes
	it just fine.)  It has an unmentioned feature.  The command

	    explain int (*f())[30]

	yields

	    declare f as function returning pointer to array 30 of int

	which is legitimate input for the "encode half" of cdecl.  The
	result of

	    declare fv as array of function returning int

	is

	    Warning: Unsupported in C -- Array of function int fv[]();

	Similar warnings exist for non-scalar cast-types, functions
	returning non-scalars (structs/unions excepted), conflicting
	type-specifiers, and improper array dimensioning (e.g.
	resulting in an array component of unspecified size).

	Problems?  The explain command doesn't understand casts yet.
	Enums are unknown.  Bit-fields are not supported.  (I don't
	really expect to include them.  The main problem with C is
	figuring out how to utter a type, not how to connect a name to
	it after it's been uttered.)  The original approach was to
	actually create a template for a C program from this
	quasi-English description of types.  That approach has left
	some dingleberries behind in the form of seemingly random
	braces and semicolons.

	I would spend some time on it before unleashing it to the net,
	but spare time is not forthcoming and it is useable in its
	current form.  It is a yacc/lex thing with a little appended
	code.

	Do you want this thing?  Let me know.  There are no
	restrictions on it.


Actually, this exercise was inspired by the memory of one quite
competant C programmer who spent about 30 minutes trying to figure out
how to declare a function as type 'function returning a pointer to a
function returning a pointer to an int', and subsequently declare
pointers to it.  This was in the days before typedefs.  Rather than
send in answers, what you were SUPPOSED to do was finger your rosaries
(or whatever) and murmer "...  Thank God for typedefs!!!" (facing
Murray Hill is optional).

	- Alan S. Watt



More information about the Comp.lang.c mailing list