Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Function calls using variables

Reply
Thread Tools

Function calls using variables

 
 
simonjpaulger@hotmail.com
Guest
Posts: n/a
 
      05-30-2005
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

 
Reply With Quote
 
 
 
 
bjrnove
Guest
Posts: n/a
 
      05-30-2005
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

 
Reply With Quote
 
 
 
 
Jonathan Bartlett
Guest
Posts: n/a
 
      05-30-2005
> 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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-30-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
(E-Mail Removed)lid

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      05-30-2005

<(E-Mail Removed)> 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.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-30-2005
"Malcolm" <(E-Mail Removed)> writes:
> <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      05-30-2005

"Keith Thompson" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      05-31-2005
In article <(E-Mail Removed)>,
Eric Sosman <(E-Mail Removed)> wrote:
>(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extracting variables accessed and written from function / rule-basedfunction calls Daniel Python 4 11-02-2010 02:57 PM
Local variables persist across function calls Dave Python 2 05-15-2010 06:01 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
This function has an onClick event that calls a function that calls This function Bob Javascript 5 10-24-2006 04:11 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM



Advertisments