Function calls using variables

S

simonjpaulger

In PHP (and other languages too, im not sure), you can call a function
using a variable itself, the variables string contains the function
name, which in turn runs.

In my situation, i have an xml file with a common descriptive name, and
a function name. I want to be able to parse the xml file and call the
function name defined in the xml, just like php can call functions with
variables. I've checked both The C Programming Language by K&R and the
internet in general but can not find anything, but im not sure im
looking with the right terminology.

Is it possible in C to call functions using variable strings?

Can anyone point me in the right direction?

Many thanks,
Brilte
 
B

bjrnove

Hi.

No, not really. You could make something similar, but nothing really
generic.

#define MAX_FUNC_NAME_LENGHT 64

/* All the functions vould have to have this initializer */
typedef int(MYSTDFUNC)(void*)

/* This struct will store the functions */
typedef struct tagFUNCTION
{
char szName[MAX_FUNC_NAME_LENGHT];
MYSTDFUNC func;
} FUNCTION, *LPFUNCTION;

/* Create an array of all the functions */
FUNCTION functions[] = {
{"Func1", Func1},
{"Func2", Func2}
};

Now all you would have to do is search trough the array of functions
until the name maches.

The problem with this way of doing things would be that the declaration
would have to be the same for all the functions. This is why I chose
void*, so that I f.eg could pass a struct with whatever arguments I
need.
 
J

Jonathan Bartlett

Is it possible in C to call functions using variable strings?

Not really. In C all of the names get stripped off during compilation.
All that's left are the addresses of the functions in memory.
However, if you create a dynamic library, the names of the exported
library functions are usually encoded in the library. The way to access
these functions vary by platform, but you can, for example, check out
dlopen/dlsym on Linux.

Jon
 
E

Eric Sosman

In PHP (and other languages too, im not sure), you can call a function
using a variable itself, the variables string contains the function
name, which in turn runs.
[...]
Is it possible in C to call functions using variable strings?

Can anyone point me in the right direction?

This is Question 20.6 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html
 
M

Malcolm

Is it possible in C to call functions using variable strings?

Can anyone point me in the right direction?
You best bet is probably to write the function in C, and then use some
existing system that implements dynamic binding by name at runtime, and can
interface to libraries with C linkage.

To implement call by name in C, you would have to have a list of names and
function pointers, and then retrive the a function pointer to the function.
If you don't know the parameters the function takes at compile time, it is
virtually impossible to build an argument list using C, though it can be
done in assembler.

It is so clumsy that you'd have to have a really good reason for attempting
this. If you want to call functions by name, C isn't the language to do it
in.
 
K

Keith Thompson

Malcolm said:
You best bet is probably to write the function in C, and then use some
existing system that implements dynamic binding by name at runtime, and can
interface to libraries with C linkage.

To implement call by name in C, you would have to have a list of names and
function pointers, and then retrive the a function pointer to the function.
If you don't know the parameters the function takes at compile time, it is
virtually impossible to build an argument list using C, though it can be
done in assembler.

The phrase "call by name" usually refers to an argument passing
convention in which argument expressions are not evaluated before the
call. This goes back to Algol 60; C doesn't support it.
 
M

Malcolm

Keith Thompson said:
The phrase "call by name" usually refers to an argument passing
convention in which argument expressions are not evaluated before the
call. This goes back to Algol 60; C doesn't support it.
Dynamic runtime binding by name, then.
 
K

Kenny McCormack

In PHP (and other languages too, im not sure), you can call a function
using a variable itself, the variables string contains the function
name, which in turn runs.
[...]
Is it possible in C to call functions using variable strings?

Can anyone point me in the right direction?

This is Question 20.6 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

(Which basically says the same thing as the first response here - build up
a table mapping strings to function pointers and go from there)

I'm surprised nobody has given the obvious and correct answer - which is:

Ask in comp.unix.programmer and I'll tell you whatever you want to know.

Note: It is actually quite easy (and fun!) to do this - on supported
platforms.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top