[comp.lang.c] Obfuscated code

woodman@sumax.UUCP (David Woodman) (12/05/89)

Hello,
  I ran across a piece of code I am unable to decipher. I wonder if
anyone else can figure this one out.

  struct MYSTRUCT someVariable;

  someVariable = (*((struct MYSTRUCT *)(*)()_msg))(parameters,...);

to quote: "The effect of this bizarre construction is to typecast the
meaning of the symbol, _msg (as opposed to the value returned by the 
function, _msg()), from pointer-to-function-returning-id to
pointer-to-function-returning-MYSTRUCT.  This is based on the fact that
C treats names as legal expressions and therefore a valid subject for a
typecast, including even the name part of the function in an expression
calling that function."

NOTE:  the called functions default to a return type of 'id'

This much I have figured out:

 (*((struct MYSTRUCT *) ...

the part   (*)()_msg    has me stumped.

P.S.  This comes out of 'Object Oriented Programming' by  Brad J. Cox
      Chapter 5, page 86    (ISBN0-201-10393-1) 
      Addison-Wesley, 1987
 
-- 
------------------------------------------------------------------------
David Woodman          MAIL: woodman%sumax.uucp@beaver.cs.washington.edu
Seattle University     #include <disclaimer.std>

chris@mimsy.umd.edu (Chris Torek) (12/05/89)

In article <1123@sumax.UUCP> woodman@sumax.UUCP (David Woodman) writes
[that he found code reading as follows]:
>  struct MYSTRUCT someVariable;
>  someVariable = (*((struct MYSTRUCT *)(*)()_msg))(parameters,...);
[there must be a typo here]

>to quote: "The effect of this bizarre construction is to typecast the
>meaning of the symbol, _msg (as opposed to the value returned by the 
>function, _msg()), from pointer-to-function-returning-id to
>pointer-to-function-returning-MYSTRUCT.

((struct MYSTRUCT *)(*)()) is over-parenthesised%---it can be written as
(struct MYSTRUCT *(*)())---but it casts not to `pointer to function
returning struct MYSTRUCT', but rather to `pointer to function
returning pointer to struct MYSTRUCT'.  The thing being cast (_msg)
should, for portability, have type `pointer to function returning <any
type>' (apparently it does).

>This much I have figured out:
> (*((struct MYSTRUCT *) ...
>the part   (*)()_msg    has me stumped.

That part is integral to the other part; you cannot figure out what
the first bit means without it.  In particular, it is not

	(*)()_msg

but rather

	(*)())_msg

(note the `lonely' close parenthesis).  If this is wrong in the book,
it may be the source of your confusion.

The simplified cast is

	(struct MYSTRUCT *(*)())

which parses as

	(*)			pointer to
	()			function [unknown arguments] returning
	*			pointer to
	struct MYSTRUCT		struct MYSTRUCT

This thing sits in the context

	(*THING)(parameters)

which is simply a K&R-1 style function call to the function pointed to
by the thing.  Note that the return type of this function is `pointer
to struct MYSTRUCT', while the lhs of the assignment statement at the
top of this message has type `struct MYSTRUCT', so the compiler should
emit an error message.  To correct it, either `someVariable' must be
declared as a pointer to struct MYSTRUCT, or _msg must be cast to

	(struct MYSTRUCT (*)())

which is `pointer to function returning struct MYSTRUCT'.
-----
% Believe it or not, `over-parenthesised' was the first thought I had
  when I read the call, and I had to mentally remove (to split an infinitive)
  the extra parentheses to decide just where to start.  After that,
  deciphering the intended cast was easy, and the incorrectly closed
  parenthesis was not apparent at first.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris

akcs.shaulp@tronsbox.xei.com (Shaul Peleg) (08/12/90)

 Can anyone direct me to the code from the Obfuscated C contest?

 Thanks,
 Shaul Peleg

andrew@csd4.csd.uwm.edu (Andrew David Biewer) (08/13/90)

akcs.shaulp@tronsbox.xei.com (Shaul Peleg) writes:
> 
>  Can anyone direct me to the code from the Obfuscated C contest?
> 

Me too?  Actually, is this posted on some newsgroups somewhere?