[comp.lang.c] strings & function addresses

wmark@wb3ffv.ampr.org (Mark Winsor) (10/18/90)

  I need to write a C front end for a cobol application. The cobol programs
will return a string that is the name of the next program to call which is
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
application in C, time doesn't allow that option.


Thanks.

Mark S. Winsor
ProVAR, Inc.

wallace@ynotme.enet.dec.com (Ray Wallace) (10/20/90)

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
---

gordon@osiris.cso.uiuc.edu (John Gordon) (10/20/90)

	You could have an array of structs, with a function pointer and a 
char string.  The string is the name of the function, and the pointer is the
address of that function.  Read in the string, compare it to the structs, and
execute the corresponding function pointer.

ttobler@unislc.uucp (Trent Tobler) (10/27/90)

From article <3785@wb3ffv.ampr.org>, by wmark@wb3ffv.ampr.org (Mark Winsor):
> 
>   I need to write a C front end for a cobol application. The cobol programs
> will return a string that is the name of the next program to call which is
> 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
> application in C, time doesn't allow that option.
> 

I'm not sure I understand the question, but as I understand it, you require
a 'C' function to be associated with a string?  If you can get that to a 
'C' string, you can use either an array or linked list of structures with
a string and a function pointer.  Here is an examples, using arrays...

/*------------------------*/
f1()
{
  ...
}

f2()
{
  ...
}

f3()
{
  ...
}

struct func_s {
	char *name;
	int (*code)();
}
myfuncs[] = {
	{ "function1", f1},
	{ "function2", f2},
	{ "function3", f3}
};

/* then to find a call a function by string ... */
find( string)
	char *string;
{
	int i;

	for( i = 0; i < sizeof( myfuncs) / (sizeof( *myfuncd); i++)
		if( !strcmp( myfuncs[i].name, string)
			return (myfuncs[i].code)();
	/* string was not in list, print an error */
	return 0;
}



-------------------------------------------------------
       ___   ___
	Trent Tobler	ttobler@csulx.weber.edu		Internet