strings & function addresses

Ray Wallace wallace at ynotme.enet.dec.com
Sat Oct 20 05:14:07 AEST 1990


In article <3785 at wb3ffv.ampr.org>, wmark at 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 at oldtmr.enet.dec.com
		(UUCP)		...!decwrl!oldtmr.enet!wallace
		(INTERNET)	wallace%oldtmr.enet at decwrl.dec.com
---



More information about the Comp.lang.c mailing list