473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1531
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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"...
5
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...
1
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 ...
2
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...
5
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...
11
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. ...
7
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...
2
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...
0
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;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.