473,659 Members | 2,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,arg 3" , for this
pair function call must be
fun1(arg1,arg2, arg3);

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

i.e.,

get fun_name,args;
form a call

Regards,
Onkar



Jun 27 '08 #1
11 1565
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,arg 3" , 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,arg 3" , for this
pair function call must be
fun1(arg1,arg2, arg3);

assume that fun1(int,char,c har*) 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] = getNameFromData base();
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.comwrot e in message
news:6f******** *************** ***********@k37 g2000hsf.google groups.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,arg 3" , for this
pair function call must be
fun1(arg1,arg2, arg3);

assume that fun1(int,char,c har*) 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,arg 3" ,
for this pair function call must be
fun1(arg1,arg2, arg3);

assume that fun1(int,char,c har*) 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.co mwrote:
<onkar.n.maha.. .@gmail.comwrot e in message

news:6f******** *************** ***********@k37 g2000hsf.google groups.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
misunderstundin g: 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******** *************** ***********@k37 g2000hsf.google groups.com...
On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.co mwrote:
><onkar.n.maha. ..@gmail.comwro te in message

news:6f******* *************** ************@k3 7g2000hsf.googl egroups.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
misunderstundin g: 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.co mwrote:
<vipps...@gmail .comwrote in message

news:e3******** *************** ***********@k37 g2000hsf.google groups.com...
On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.co mwrote:
<onkar.n.maha.. .@gmail.comwrot e in message
>news:6f******* *************** ************@k3 7g2000hsf.googl egroups.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
misunderstundin g: 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.co mwrote:
><vipps...@gmai l.comwrote in message

news:e3******* *************** ************@k3 7g2000hsf.googl egroups.com...
On Jun 20, 2:29 pm, "Bartc" <b...@freeuk.co mwrote:
<onkar.n.maha. ..@gmail.comwro te in message
>>news:6f****** *************** *************@k 37g2000hsf.goog legroups.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
misunderstundin g: 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1252
by: michael | last post by:
Hi, below is a snipplet that could be seen as a part of a spreadsheet with getter and setter properties and a way how to dynamically insert function to be used when setting the value of a "cell" instance import new import inspect
5
2246
by: Sarir Khamsi | last post by:
I have a class (Command) that derives from cmd.Cmd and I want to add methods to it dynamically. I've added a do_alias() method and it would be nice if I could turn an alias command into a real method of Command (that way the user could get help and name completion). The code would be generated dynamically from what gets passed to the do_alias() method. I've tried looking in the Python cookbook and have tried: def funcToMethod(func, clas,...
1
2579
by: Gopal Krish | last post by:
I'm have coded a simple menu (using link buttons as menu items) in a user control to be reused across many ASPX pages. In the page_load method I dynamically create the link buttons as follows LinkButton myLB = new LinkButton(); ......... .........
2
1532
by: Ed Leafe | last post by:
Hi, Thanks to the help of many on this list, I've been able to take code that is created by the user in my app and add it to an object as an instance method. The technique used is roughly: nm = "myMethod" code = """def myMethod(self): print "Line 1" print "My Value is %s" % self.Value
5
2239
by: Chris Lieb | last post by:
I am trying to add an event listener to the keyup event for some text inputs that I am creating dynamically. I have no problems getting it to work in Firefox, but IE just ignores them. I have created a few functions to aid in making this process work semi-cross-browser. (I develop in FF, but everyone who uses it will be using IE6.) function attachEvent(_obj, _evt, _fnc) { _evt = _evt.toLowerCase(); if (_obj.attachEvent) {
11
3007
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. It works perfectly (assigning the correct images and the correct onclick events to the correct <atags):
7
1243
by: MRW | last post by:
Hello! I'm trying to do something very simple. On the bottom of my page, I want to dynamically create and display something like (in hyperlink text): Home | Page 1 and if x = 1 (for example), I want it do display:
2
1284
by: Nick Keighley | last post by:
On 20 Jun, 10:29, onkar.n.maha...@gmail.com wrote: no C is statically compiled. By the time it executes everything must be there and types are fixed. Dynamically linked libraries can enable you to change functionality on the fly but the function call interface remains fixed.
0
1105
by: Gabriel Genellina | last post by:
En Tue, 29 Jul 2008 01:17:13 -0300, Piyush Anonymous <piyush.subscription@gmail.comescribi�: You forget the self argument (this explains the error you got). It's a lot easier than that; just add the *function* object to the class namespace: setattr(clas, method_name, func)
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.