Path: utzoo!attcan!uunet!lll-winken!lll-lcc!ames!ll-xn!mit-eddie!uw-beaver!uw-june!pardo
From: pardo@june.cs.washington.edu (David Keppel)
Newsgroups: comp.lang.c
Subject: void* vs. pointer-to-function
Summary: How do I do this?
Message-ID: <5304@june.cs.washington.edu>
Date: 16 Jul 88 04:20:13 GMT
References: <5301@june.cs.washington.edu>
Reply-To: pardo@cs.washington.edu (David Keppel)
Organization: U of Washington, Computer Science, Seattle
Lines: 42
Disorganization: U of Washington, Computer Science, Seattle

Assume for the moment that I have a compiler that takes
prototypes, and knows about void* and all that jazz.

I want to write a function that can return pointers to
semi-arbitrary functions.  I want to do something like:
(Can you say "run-time linker"?  I *knew* you could!)

    typedef void (*a_type)();
    tyepdef int (*b_type)(int a, char *b[]);
    typedef struct splat (*c_type)( struct splat a, char *s);

    void *func_returning_func( int );
    struct splat bork = {...};

	:
	void *ptr
	:
	ptr = func_returning_func( tag );
	switch( tag ) {
	    case 1:
		(*(a_type)ptr)();
		break;
	    case 2:
		s = (*(b_type)ptr)(2, "2");
		break;
	    case 3:
		bork = (*(c_type)ptr)( bork, "3" );
	}
	:

Given that "bork" is initialized properly, will this all
work?  Portably?  Even on machines that pass parameters in
registers unless (like bork) it is too big to fit in a
single register?

If this is broke, how can I do what I want?

E-mail, please.

	    ;-D on  ( Ok, now how about this: )  Pardo

		    pardo@cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo