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 7 1444
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
> 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 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
<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.
"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.
"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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Penn Markham |
last post by:
Hello all,
I am writing a script where I need to use the system() function to call
htpasswd. I can do this just fine on the command line...works great
(see attached file, test.php). When my...
|
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {...
|
by: Michael |
last post by:
Guys,
I'm interested in how the compiler implements function calls, can anyone
correct my understanding/point me towards some good articles.
When a function is called, is the stack pointer...
|
by: Paul M |
last post by:
Hi,
This is on an AS/400 which can be a little strange but I think the
basic question is portable.
I have a (non-C) program that needs to make series of calls
to some C programs/functions....
|
by: anonymous |
last post by:
Thanks your reply.
The article I read is from
www.hakin9.org/en/attachments/stackoverflow_en.pdf.
And you're right. I don't know it very clearly. And that's why I want
to understand it; for it's...
|
by: CES |
last post by:
All,
I was wondering if someone could point me to a tutorial on creating & accessing functions from within a wrapper function.
I've created a group of functions related to a timer event. All...
|
by: Sandra-24 |
last post by:
Is there a way in python to add the items of a dictionary to the local
function scope? i.e. var_foo = dict. I don't know how many
items are in this dictionary, or what they are until runtime.
...
|
by: Jeff |
last post by:
Hello,
I assigned a new object to a local variable ("req") in a function (see
below). The local variable "req" is obviously destroyed when the
function exits, but should the object referenced by...
|
by: Larax |
last post by:
Best explanation of my question will be an example, look below at this
simple function:
function SetEventHandler(element)
{
// some operations on element
element.onclick =
function(event)
{
|
by: Why Tea |
last post by:
I seem to remember that in ANSI C, all static functions should have
their function prototypes listed at the beginning of the file for
better consistency (or something like that). I googled and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |