strings & function addresses
Trent Tobler
ttobler at unislc.uucp
Sat Oct 27 07:15:32 AEST 1990
>From article <3785 at wb3ffv.ampr.org>, by wmark at 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 at csulx.weber.edu Internet
More information about the Comp.lang.c
mailing list