469,575 Members | 1,668 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Interesting problem dynamically forming func names ??

Is it possible to from function call on fly in C programming
language ?

I am faced with an interesting problem in that I need to take function
name and arguments on fly (actually from Database as string ) and form
a function call based on that ?
something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" , for this
pair function call must be
fun1(arg1,arg2,arg3);

assume that fun1(int,char,char*) is defined before the call ?

i.e.,

get fun_name,args;
form a call

Regards,
Onkar



Jun 27 '08 #1
11 1292
On 20 Jun 2008 at 9:29, on*************@gmail.com wrote:
Is it possible to from function call on fly in C programming
language ?

something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" , for this
pair function call must be
fun1(arg1,arg2,arg3);
The main choices are: maintain a table of function pointers and hash
FUN_NAME to an index in the table, or use a function like dlsym() to
access the symbol table itself.

Jun 27 '08 #2
On Jun 20, 2:29 pm, onkar.n.maha...@gmail.com wrote:
Is it possible to from function call on fly in C programming
language ?

I am faced with an interesting problem in that I need to take function
name and arguments on fly (actually from Database as string ) and form
a function call based on that ?

something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" , for this
pair function call must be
fun1(arg1,arg2,arg3);

assume that fun1(int,char,char*) is defined before the call ?

i.e.,

get fun_name,args;
form a call

Regards,
Onkar
/* Not compiled */
#include <stdio.h>
#include <stdlib.h>

#define NAME_LEN ..
#define NUM ..

struct _node {
char name[NAME_LEN];
void (*fnPtr)(void);
}node;

int foo(int a);
void bar(char *a, float b);
double baz(void );

int
main(void) {
struct node table[NUM] = { {"foo", &foo}, {"bar", &bar}, {"baz",
&baz} };
char name[NAME_LEN] = getNameFromDatabase();
int count = 0;
for ( ; count < NUM; count++) {
if (strcmp(name, table.name) == 0) {
table.fnPtr(/*place the arguments */);
}
}
return 0;
}
This is a very crude approach. You can refine on this( use link list
or hash tables in place of arrays, do something for parameters etc)
but the basic framework is going to be the same.

There aren't any generic function pointers in C (void * can stand only
for objects) but a pointer to a function can stand for any other
function type. You can use some casting to shut the compiler up.
Jun 27 '08 #3

<on*************@gmail.comwrote in message
news:6f**********************************@k37g2000 hsf.googlegroups.com...
Is it possible to from function call on fly in C programming
language ?

I am faced with an interesting problem in that I need to take function
name and arguments on fly (actually from Database as string ) and form
a function call based on that ?
something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" , for this
pair function call must be
fun1(arg1,arg2,arg3);

assume that fun1(int,char,char*) is defined before the call ?
A table of function names and addresses has been mentioned.

If you are using Windows and the functions are in a DLL file, then they are
often accessed by name anyway:

GetProcAddress(hinst,"ijlInit")

and perhaps there might be something similar on your system.

If the functions could have different parameter sets and return values,
that's an extra complication; you will need to classify them according to
their signatures and call each group separately.

How many functions are we talking about? The function name cannot be
arbitrary because it must already exist somewhere (in most languages not
just C).

--
Bartc

Jun 27 '08 #4
on*************@gmail.com wrote:
Is it possible to from function call on fly in C programming
language ?

I am faced with an interesting problem in that I need to take
function name and arguments on fly (actually from Database as
string ) and form a function call based on that ?
something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" ,
for this pair function call must be
fun1(arg1,arg2,arg3);

assume that fun1(int,char,char*) is defined before the call ?
The one problem is getting the address of the function code by
only the name, the other problem is building an arbitrary
function call at runtime.

The first problem is addressed by dynamic linkers: If your
program exports it's symbols you can do a dlsym (*nix) or
GetProcAddress (Windows) on it. Or you add some symbol table at
compile time

typedef struct symbol
{
char const * const name;
void * const p;
} symbol;
symbol symbols[] =
{
{"bla", bla},
{"foo", foo},
{"bar", bar},
...
};

The second one _can not_ be gernerically addressed with C alone!
Although you can define a whole bunch of function prototype
types and use some very large switch structure to implement it
to some degree. It is however in this case a lot more easier -
if you know the architecture - to dynamically build the call
frame using assembler. And since such is a usefull thing for
e.g. script language interpreters, someone has implemented it
and provides it as a part of the ffcall library:

http://www.haible.de/bruno/packages-ffcall.html

HTH

Wolfgang Draxinger
--
E-Mail address works, Jabber: he******@jabber.org, ICQ: 134682867

Jun 27 '08 #5
On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.comwrote:
<onkar.n.maha...@gmail.comwrote in message

news:6f**********************************@k37g2000 hsf.googlegroups.com...
Is it possible to from function call on fly in C programming
language ?
It's not possible. There are workarounds, Mr rahul posted an example
of one.
<snip>
A table of function names and addresses has been mentioned.
The person who mentioned it is a troll (just so there isn't a
misunderstunding: I'm talking about Antoninus Twink). Don't advice
people to look at posts made my trolls.
If you are using Windows and the functions are in a DLL file, then they are
often accessed by name anyway:

GetProcAddress(hinst,"ijlInit")
Which is not topical in comp.lang.c. Please stop posting off-topic
advice.
<snip>
Jun 27 '08 #6
<vi******@gmail.comwrote in message
news:e3**********************************@k37g2000 hsf.googlegroups.com...
On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.comwrote:
><onkar.n.maha...@gmail.comwrote in message

news:6f**********************************@k37g200 0hsf.googlegroups.com...
Is it possible to from function call on fly in C programming
language ?
It's not possible. There are workarounds, Mr rahul posted an example
of one.
<snip>
>A table of function names and addresses has been mentioned.
The person who mentioned it is a troll (just so there isn't a
misunderstunding: I'm talking about Antoninus Twink). Don't advice
people to look at posts made my trolls.
Nevertheless, his advice (also repeated in the thread) was good.
>If you are using Windows and the functions are in a DLL file, then they
are
often accessed by name anyway:

GetProcAddress(hinst,"ijlInit")
Which is not topical in comp.lang.c. Please stop posting off-topic
advice.
Just trying to help. And I said "if".

Anyway this was also mentioned in this thread (by Wolfgang Draxinger), but
strangely you have not ticked him off.

So why pick on me?

--
Bartc
Jun 27 '08 #7
On Jun 20, 4:22 pm, "Bartc" <b...@freeuk.comwrote:
<vipps...@gmail.comwrote in message

news:e3**********************************@k37g2000 hsf.googlegroups.com...
On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.comwrote:
<onkar.n.maha...@gmail.comwrote in message
>news:6f**********************************@k37g200 0hsf.googlegroups.com...
Is it possible to from function call on fly in C programming
language ?
It's not possible. There are workarounds, Mr rahul posted an example
of one.
<snip>
A table of function names and addresses has been mentioned.
The person who mentioned it is a troll (just so there isn't a
misunderstunding: I'm talking about Antoninus Twink). Don't advice
people to look at posts made my trolls.

Nevertheless, his advice (also repeated in the thread) was good.
It was not, Antoninus also mentioned dlsym, which is not ISO C.
Antoninus doesn't care about advice, he just wants to post off-topic
crap to disrupt the newsgroup.
If you are using Windows and the functions are in a DLL file, then they
are
often accessed by name anyway:
GetProcAddress(hinst,"ijlInit")
Which is not topical in comp.lang.c. Please stop posting off-topic
advice.

Just trying to help. And I said "if".
I understand your intentions were genuine but still, please try to
avoid posting off-topic advice.
"if" is not the same with "The following is not topical here in
comp.lang.c and the ISO C standard does not mention such function",
who knows what OP might think...
Anyway this was also mentioned in this thread (by Wolfgang Draxinger), but
strangely you have not ticked him off.

So why pick on me?
Actually I did not see Mr Draxingers post, but now that I read it, I
shall advice him the same, to not post off-topic replies/help.
It's best to remain topical, it keeps trolling noise to a minimum.
Jun 27 '08 #8
vi******@gmail.com writes:
On Jun 20, 4:22 pm, "Bartc" <b...@freeuk.comwrote:
><vipps...@gmail.comwrote in message

news:e3**********************************@k37g200 0hsf.googlegroups.com...
On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.comwrote:
<onkar.n.maha...@gmail.comwrote in message
>>news:6f**********************************@k37g20 00hsf.googlegroups.com...
Is it possible to from function call on fly in C programming
language ?
It's not possible. There are workarounds, Mr rahul posted an example
of one.
<snip>
A table of function names and addresses has been mentioned.
The person who mentioned it is a troll (just so there isn't a
misunderstunding: I'm talking about Antoninus Twink). Don't advice
people to look at posts made my trolls.

Nevertheless, his advice (also repeated in the thread) was good.
It was not, Antoninus also mentioned dlsym, which is not ISO C.
Antoninus doesn't care about advice, he just wants to post off-topic
crap to disrupt the newsgroup.
>If you are using Windows and the functions are in a DLL file, then they
are
often accessed by name anyway:
>GetProcAddress(hinst,"ijlInit")
Which is not topical in comp.lang.c. Please stop posting off-topic
advice.

Just trying to help. And I said "if".
I understand your intentions were genuine but still, please try to
avoid posting off-topic advice.
Get a life you miserable jobs worth. Your attempts at cosying up to the
clique are quite nauseating. If the replies give HELP then all well and
good. It might surprise you but a LOT of people coming to this group
dont give a rats arse about having C code which can compile on 20000
different systems. They are C programmers looking for some help. If
there is an alternative ISO compliant solution then why do you not give
it? Probably because like Default User and "Chuck" more of your posts
are pedantic nit picking and net nannying than offering any real advice.
"if" is not the same with "The following is not topical here in
comp.lang.c and the ISO C standard does not mention such function",
who knows what OP might think...
>Anyway this was also mentioned in this thread (by Wolfgang Draxinger), but
strangely you have not ticked him off.

So why pick on me?
Actually I did not see Mr Draxingers post, but now that I read it, I
Aha. The old "Mr". Clearly we have a nym shifter in our midst.
shall advice him the same, to not post off-topic replies/help.
It's best to remain topical, it keeps trolling noise to a minimum.
It would bet better for a lot of people if you would keep you trolling
net nannying down too.

Jun 27 '08 #9
On Jun 20, 5:29 am, onkar.n.maha...@gmail.com wrote:
I am faced with an interesting problem in that I need to take function
name and arguments on fly (actually from Database as string ) and form
a function call based on that ?
Hello Onkar, List

Can you please, elaborate? Where are that function's when you first
compile the application? It is from some dynamic lib? Did you consider
use some embedded script language, or maybe embed some c compiler to
run the code on the fly? You *NEED* to do that, as assignment, or you
just think that is the solution to your problem (and really... so far,
I dont know what problem is it)? Maybe we can change your question a
little bit, to find a better answer... :o

Regards
Rafael
Jun 27 '08 #10
On 20 Jun, 12:29, "Bartc" <b...@freeuk.comwrote:

<snip>
The function name cannot be
arbitrary because it must already exist somewhere (in most languages not
just C).
but not all. Or rather it doesn't have to exist at compile time.

--
Nick Keighley
Jun 27 '08 #11
On Jun 20, 5:29 pm, onkar.n.maha...@gmail.com wrote:
Is it possible to from function call on fly in C programming
language ?

I am faced with an interesting problem in that I need to take function
name and arguments on fly (actually from Database as string ) and form
a function call based on that ?

something like FUN_NAME="fun1" , ARGS ="arg1,arg2,arg3" , for this
pair function call must be
fun1(arg1,arg2,arg3);

assume that fun1(int,char,char*) is defined before the call ?

i.e.,

get fun_name,args;
form a call

Regards,
Onkar
I've thought about a problem like this.
it is my high school career when i was studying pascal language.
that problem is i wanna such fun1(array[1..n])
the n is variable.but according pascal law, the variable couldn't used
in arrays.
but in a function(*), i could make n a concrete number. and make fun1
be a function in this fuction(*).

it works...
Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by michael | last post: by
5 posts views Thread by Sarir Khamsi | last post: by
2 posts views Thread by Ed Leafe | last post: by
7 posts views Thread by MRW | last post: by
reply views Thread by Gabriel Genellina | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.