Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!olivea!samsung!dali.cs.montana.edu!uakari.primate.wisc.edu!aplcen!haven!decuac!shlump.nac.dec.com!engage.enet.dec.com!ynotme.enet.dec.com!wallace From: wallace@ynotme.enet.dec.com (Ray Wallace) Newsgroups: comp.lang.c Subject: Re: strings & function addresses Message-ID: <2103@engage.enet.dec.com> Date: 19 Oct 90 19:14:07 GMT Sender: news@engage.enet.dec.com Organization: Digital Equipment Corporation Lines: 51 In article <3785@wb3ffv.ampr.org>, wmark@wb3ffv.ampr.org (Mark Winsor) writes... >representing a C function. I need a way to associate that string with the >function address, anybody have any ideas? Please don't suggest I rewrite the You need a structure which contains a function address and a char pointer to the function name. Initialize an array of these structures (size of array dependent on how many functions you have). When you get the name of a function just walk through the array comparing the name with the name in each structure of the array, when you find the name then call the function address in that structure. Something like typedef struct { int (*func_addr)(); /* Pointer to function returning int*/ char *func_name; /* Pointer to function name */ } FUNCTIONS; int func_1(); int func_2(); int func_3(); FUNCTIONS list[] = { {func_1(), "func_1"}, {func_2(), "func_2"}, {func_3(), "func_3"}, {NULL, NULL} /* Indicate end of list */ }; call_func( name ) char *name; { FUNCTIONS *next; for( next=list; next->func_addr && strcmp(name, next->func_name)==0; ++next); if( next ) next->func_addr(); /* Some compilers require this instead */ if( next ) (*next->func_addr)(); } Note that I didn't test this code, I just typed it in quick, so no guarantees on it's accuracy but it should give you the idea of how to do it. --- Ray Wallace (INTERNET,UUCP) wallace@oldtmr.enet.dec.com (UUCP) ...!decwrl!oldtmr.enet!wallace (INTERNET) wallace%oldtmr.enet@decwrl.dec.com ---