469,343 Members | 5,762 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

Function calls using variables

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

Nov 14 '05 #1
7 1283
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.

--
bjrnove

Nov 14 '05 #2
> 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
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #3
si***********@hotmail.com wrote:
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

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 14 '05 #4

<si***********@hotmail.com> wrote

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.
Nov 14 '05 #5
"Malcolm" <re*******@btinternet.com> writes:
<si***********@hotmail.com> wrote

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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

"Keith Thompson" <ks***@mib.org> wrote

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.
Nov 14 '05 #7
In article <A_********************@comcast.com>,
Eric Sosman <es*****@acm-dot-org.invalid> wrote:
si***********@hotmail.com wrote:
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.

Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Penn Markham | last post: by
3 posts views Thread by domeceo | last post: by
10 posts views Thread by Michael | last post: by
5 posts views Thread by Sandra-24 | last post: by
28 posts views Thread by Why Tea | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.