Casting function ptrs

Dave Hammond daveh at marob.MASA.COM
Thu May 19 06:46:40 AEST 1988


Given a list of varying typed functions, their names and addresses:

struct funcs {
    char *name;
    unsigned (*func)();
}
char *foo();
struct funcs f = {  "foo", (char *)foo };

And a routine which is passed a function name in order to lookup and return
its associated address: 

unsigned *lookup_func(name)
char *name;
{
  ...
  return( (unsigned *) f->func );
}

How does one declare the lookup function, the return address variable and
call the returned function, while properly casting the function return value?
I'm doing it like so:

unsigned *lookup_func();
unsigned *(*func)();
char *retp;

if ((func = lookup_func("foo")) != (unsigned *)0) {
    if (( retp = (char *) (*func)() ))
	printf("retp=%s",retp);
    }

Yes? No? Gonna blow up on me at some point ?


Dave Hammond
DSI Communications, Inc.
--------------------------------
UUCP:   ...!marob!daveh
VOICE:  (516)872-3535
USMAIL: 333 W. Merrick Rd.
        Valley Stream, NY 11580
--------------------------------



More information about the Comp.unix.questions mailing list