[comp.lang.c] Calling functions in C....

segel@infmx.UUCP (Mike Segel) (08/15/89)

Hi, It has been a while since I have posted to this group,
but I have a strange question....

	I want to write a C function which will be passed a set of strings.
The C function would then use the first parameter as the function name,
and the other parameters as the variables. 
	I can figure out how to get the variables passed, but I cannot
think of a way to handle the function name.

Has anyone tried this or can think of a way of doing it? (Besides 
of a large switch statement?)

_Mike Segel
Informix Software
segel@infmx

-- 
Mike Segel
Chicago CN, MIS

Informix Software Inc.

poser@csli.Stanford.EDU (Bill Poser) (08/15/89)

In article <2108@infmx.UUCP> segel@infmx.UUCP (Mike Segel) writes:

>	I want to write a C function which will be passed a set of strings.
>The C function would then use the first parameter as the function name,
>and the other parameters as the variables. 
>	I can figure out how to get the variables passed, but I cannot
>think of a way to handle the function name.
>

If I understand the problem correctly, what is desired is to map strings
to function addresses. To do this, set up a data structure whose elements
are structures containing the function name and its address, e.g.:

	struct foo {
		char *name;		/* Name of function as string */
		int (*func)();		/* Pointer to function */
	};

If you use an array, the declaration and initialization would look like
this:

	struct foo funcmap[]={
		"function1",function1,
		"function2",function2,
		....
		"functionN",functionN
	};

Then define an access function that searches the data structure, using the
function name, and returns the pointer to the matching function (i.e.
funcmap[i].func) which you then execute via the pointer, e.g. like this:

	funcptr = LookupFunction(string);
	if(funcptr != NULL) retval = (*funcptr)(arg1,arg2,...argn);
	
The nature of the data structure and the corresponding access function
depend on the properties of the function list and how fast the search needs
to be. The easiest thing to do is to create an array of structs and use
linear search. If you know the names of all the functions at compile time
and can make sure they are correctly ordered you can do binary search
instead.  In some circumstances a hashing technique will be appropriate.

tneff@bfmny0.UUCP (Tom Neff) (08/16/89)

In article <2108@infmx.UUCP> segel@infmx.UUCP (Mike Segel) writes:
>	I want to write a C function which will be passed a set of strings.
>The C function would then use the first parameter as the function name,
>and the other parameters as the variables. 
>	I can figure out how to get the variables passed, but I cannot
>think of a way to handle the function name.

Unh-unh.  This is the sort of thing a C *interpreter* might be able to
handle, but not a compiler.  You're talking about dynamically resolving
a program symbol name at runtime (by which time, in most systems, the names
have ceased to exist).  Certain exceptional systems may offer this
feature (I am sure we will now be inundated with little twisty followups
all alike telling us about the exceptions), but mostly no.  And if it
were offered it would be plastered all over the documentation as a super
duper feature so the original posting would not have been needed. :-)

-- 
"We walked on the moon --	((	Tom Neff
	you be polite"		 )) 	tneff@bfmny0.UU.NET