By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

'dynamic' function pointer argument list

P: n/a
Hello,

I would like to build a small interpreter (in an c-program), which calls
functions with user-defined arguments.
To do this, I have a list of functions names (in an array), with the
pointers to the function (in an external library). The user-supplied
arguments I got as Strings, which i have to transform to the right type
(adresses, char, int, long, float, double etc). For each function I
have a list of the supplied argumentstype (which of course could be
different for each function).

The problem I've got is, how can I call the function with different
argumentlist?

A function-pointer is declared with a clear argument list. Also I have
to "add" the argumentlist in the source-code. But I would like to add
the argumentlist at runtime. Is there any solution for this problem?

best regards

Bernhard

Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
on 7/16/2004 5:31 PM Bernhard Wrote:
Hello,

I would like to build a small interpreter (in an c-program), which calls
functions with user-defined arguments.
To do this, I have a list of functions names (in an array), with the
pointers to the function (in an external library). The user-supplied
arguments I got as Strings, which i have to transform to the right type
(adresses, char, int, long, float, double etc). For each function I
have a list of the supplied argumentstype (which of course could be
different for each function).

The problem I've got is, how can I call the function with different
argumentlist?

A function-pointer is declared with a clear argument list. Also I have
to "add" the argumentlist in the source-code. But I would like to add
the argumentlist at runtime. Is there any solution for this problem?

best regards

Bernhard

You can try making the all the functions to be variadic.
But that would add a lot of overhead in creating/rewriting
the functions in the first place.

--
Shanmu.
Nov 14 '05 #2

P: n/a
Shanmuhanathan T wrote:
on 7/16/2004 5:31 PM Bernhard Wrote:
Hello,
....
.... You can try making the all the functions to be variadic.
But that would add a lot of overhead in creating/rewriting
the functions in the first place.


Hi Shanmuhanathan,

what does 'variadic' mean (sorry, I'm not a native speaker) ?

best regards
Bernhard

Nov 14 '05 #3

P: n/a
Bernhard <nh**@gmx.de> wrote:
I would like to build a small interpreter (in an c-program), which calls
functions with user-defined arguments.
To do this, I have a list of functions names (in an array), with the
pointers to the function (in an external library). The user-supplied
arguments I got as Strings, which i have to transform to the right type
(adresses, char, int, long, float, double etc). For each function I
have a list of the supplied argumentstype (which of course could be
different for each function). The problem I've got is, how can I call the function with different
argumentlist? A function-pointer is declared with a clear argument list. Also I have
to "add" the argumentlist in the source-code. But I would like to add
the argumentlist at runtime. Is there any solution for this problem?


The way I solved that problem in a similar project was to pass a
pointer to a (linked) list of function arguments to the functions,
where (in principle) each element of the list consists of an int
with type information and an union for the argument value, with a
member for each possible type. That way each and every function
always gets only a single argument but you still can invoke it
with an arbitrary number of arguments and types, passing all
numbers and types via the list. Each function to be called that
way has the form

struct var ( * fnct ) ( struct var * );

with the var structure being declared similar to

struct var {
int type;
union {
int int_var;
long long_var;
float float_var;
double double_var;
}
struct var *next;
};

Of course, that involves a bit of overhead, since you have to assemble
the list of arguments from the user input (and get rid of it after the
function call) but for an interpreted language that probably isn't much
of a concern...
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4

P: n/a
Bernhard wrote:
Shanmuhanathan T wrote:
on 7/16/2004 5:31 PM Bernhard Wrote:
Hello,

...
...
You can try making the all the functions to be variadic.
But that would add a lot of overhead in creating/rewriting
the functions in the first place.


Hi Shanmuhanathan,

what does 'variadic' mean (sorry, I'm not a native speaker) ?

best regards
Bernhard

Sorry, found the word variadic with no problems with google.

On the first View it locks nice, but after all, I have the 'hardcode'
the arguments in some c-file. But I don't know at compiling time the
number of arguments.

best regards
Bernhard
Nov 14 '05 #5

P: n/a
Bernhard <nh**@gmx.de> wrote:
Shanmuhanathan T wrote:
on 7/16/2004 5:31 PM Bernhard Wrote:
Hello,

...
You can try making the all the functions to be variadic.
But that would add a lot of overhead in creating/rewriting
the functions in the first place.

what does 'variadic' mean (sorry, I'm not a native speaker) ?


Functions with variadic arguments are functions like printf()
that are typically declared as

int printf( const char *format, ... );

where the three dots stand for an arbitrary number of arguments
and where the number of arguments as well as their types can be
determined from the format string (but it don't have to be a
string). The necessity for the format string results from the
fact that from within the called function you have no means
to figure out with how many arguments it was called with except
by analysing the format string.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #6

P: n/a
Je***********@physik.fu-berlin.de wrote:
Bernhard <nh**@gmx.de> wrote:
I would like to build a small interpreter (in an c-program), which calls
functions with user-defined arguments.
To do this, I have a list of functions names (in an array), with the
pointers to the function (in an external library). The user-supplied
arguments I got as Strings, which i have to transform to the right type
(adresses, char, int, long, float, double etc). For each function I
have a list of the supplied argumentstype (which of course could be
different for each function).


The problem I've got is, how can I call the function with different
argumentlist?


A function-pointer is declared with a clear argument list. Also I have
to "add" the argumentlist in the source-code. But I would like to add
the argumentlist at runtime. Is there any solution for this problem?

The way I solved that problem in a similar project was to pass a
pointer to a (linked) list of function arguments to the functions,
where (in principle) each element of the list consists of an int
with type information and an union for the argument value, with a
member for each possible type. That way each and every function
always gets only a single argument but you still can invoke it
with an arbitrary number of arguments and types, passing all
numbers and types via the list. Each function to be called that
way has the form

struct var ( * fnct ) ( struct var * );

with the var structure being declared similar to

struct var {
int type;
union {
int int_var;
long long_var;
float float_var;
double double_var;
}
struct var *next;
};

Of course, that involves a bit of overhead, since you have to assemble
the list of arguments from the user input (and get rid of it after the
function call) but for an interpreted language that probably isn't much
of a concern...
Regards, Jens


This sounds not too bad.

I could write for each Library Function an Wrapper-Function with only
parameter (like your struct var). The Wrapper-Functions knows exactly,
the number of functions arguments, so it can get the values out of the
struct, and can directly use the library function.

Yes, so it will work.

thank you very much for the idea

best regards
Bernhard

Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.