'dynamic' function pointer argument list

Discussion in 'C Programming' started by Bernhard, Jul 16, 2004.

  1. Bernhard

    Bernhard Guest

    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
     
    Bernhard, Jul 16, 2004
    #1
    1. Advertising

  2. 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.
     
    Shanmuhanathan T, Jul 16, 2004
    #2
    1. Advertising

  3. Bernhard

    Bernhard Guest

    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
     
    Bernhard, Jul 16, 2004
    #3
  4. Bernhard

    -berlin.de Guest

    Bernhard <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Jul 16, 2004
    #4
  5. Bernhard

    Bernhard Guest

    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
     
    Bernhard, Jul 16, 2004
    #5
  6. Bernhard

    -berlin.de Guest

    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.
    >>

    > 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Jul 16, 2004
    #6
  7. Bernhard

    Bernhard Guest

    -berlin.de wrote:

    > Bernhard <> 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
     
    Bernhard, Jul 16, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Kial
    Replies:
    1
    Views:
    664
    Eric Enright
    Nov 15, 2004
  2. Vijai Kalyan
    Replies:
    4
    Views:
    714
    Vijai Kalyan
    Nov 8, 2005
  3. Reckoner
    Replies:
    11
    Views:
    708
    Steven D'Aprano
    Jan 19, 2009
  4. Function pointer in argument list

    , Mar 26, 2009, in forum: C Programming
    Replies:
    3
    Views:
    328
    Ben Bacarisse
    Mar 26, 2009
  5. AikidoGuy
    Replies:
    11
    Views:
    560
    Seebs
    Nov 21, 2011
Loading...

Share This Page