How to call function whose function call with arguments is in astring Options

Discussion in 'C Programming' started by grbgooglefan, Jan 30, 2008.

  1. grbgooglefan

    grbgooglefan Guest

    Hello,
    Can you please enlighten me if we can do this in C?

    I would like to do something like this:
    ---------------------------------------------------------------------------­--------------------------
    typedef struct IOParams {
    char *ioString;
    long lionum;
    double dionum;
    float fionum;
    int nionum;


    } *pIOParams;

    char *strFunctionCmd =
    "myfunctionCall("iiisd",v_ioparams[0].nionum,v_ioparams[1].nionum,v_ioparam­
    s[2].nionum,v_ioparams[3].ioString,v_ioparams[4].dionum)";


    int retstat = call_function_in_string(strFunctionCmd );
    ---------------------------------------------------------------------------­--------------------------
    The vector size could vary, but that is not main issues here.
    The problem is that even the variable number arguments function
    cannot
    be used here because for those functions also we need to have the
    number & type of arguments fixed at compile time and the input
    arguments passed to them are individual variables.

    In the above case, variables are part of a vector having variable
    size, so writing call for that also is difficult.

    Is there any way to achieve this in C?
    Any suggestions please?
    Thanks in advance for help
     
    grbgooglefan, Jan 30, 2008
    #1
    1. Advertising

  2. grbgooglefan

    Mark Bluemel Guest

    Re: How to call function whose function call with arguments is ina string Options

    grbgooglefan wrote:
    > Hello,
    > Can you please enlighten me if we can do this in C?
    >
    > I would like to do something like this:
    > ---------------------------------------------------------------------------­--------------------------
    > typedef struct IOParams {
    > char *ioString;
    > long lionum;
    > double dionum;
    > float fionum;
    > int nionum;
    >
    >
    > } *pIOParams;
    >
    > char *strFunctionCmd =
    > "myfunctionCall("iiisd",v_ioparams[0].nionum,v_ioparams[1].nionum,v_ioparam­
    > s[2].nionum,v_ioparams[3].ioString,v_ioparams[4].dionum)";
    >
    >
    > int retstat = call_function_in_string(strFunctionCmd );


    You can't do this as such, as you should have worked out from reading
    your C textbook...

    The nearest approach is to use function pointers, perhaps in a lookup
    table, to allow you to choose a function at runtime. Argument handling
    in such a context is tricky as the number and type of arguments will be
    fixed, but creative use of pointers to structures may help.
     
    Mark Bluemel, Jan 30, 2008
    #2
    1. Advertising

  3. Re: How to call function whose function call with arguments is in a string Options

    "grbgooglefan" <> wrote in message
    >
    >The vector size could vary, but that is not main issues here.
    >The problem is that even the variable number arguments function
    >cannot be used here because for those functions also we need to have the
    >number & type of arguments fixed at compile time and the input
    >arguments passed to them are individual variables.
    >

    One of the very few things C won't let you do is build an argument list at
    runtime. So to achieve what you want in C, effectively you have to write an
    interpreter for another language.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Jan 30, 2008
    #3
  4. Malcolm McLean wrote:
    >
    > "grbgooglefan" <> wrote in message
    > >
    > >The vector size could vary, but that is not main issues here.
    > >The problem is that even the variable number arguments function
    > >cannot be used here because for those functions also we need to have the
    > >number & type of arguments fixed at compile time and the input
    > >arguments passed to them are individual variables.
    > >

    > One of the very few things C won't let you do is build an argument list at
    > runtime. So to achieve what you want in C, effectively you have to write an
    > interpreter for another language.


    Well, it is possible to pass such things by creating a struct of
    a type and a union of all possible parameter types, and pass a
    pointer to an array of this struct (along with a parameter count).
    Think of argc/argv, where argv is a "MyParam_t *" rather than a
    "char **".

    Of course, the called function needs to be written to takes its
    arguments in such a manner, so you are correct that this cannot
    be done in the general sense.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Jan 30, 2008
    #4
  5. Re: How to call function whose function call with arguments is in a string Options

    In article <>,
    Malcolm McLean <> wrote:
    >
    >"grbgooglefan" <> wrote in message
    >>
    >>The vector size could vary, but that is not main issues here.
    >>The problem is that even the variable number arguments function
    >>cannot be used here because for those functions also we need to have the
    >>number & type of arguments fixed at compile time and the input
    >>arguments passed to them are individual variables.
    >>

    >One of the very few things C won't let you do is build an argument list at
    >runtime. So to achieve what you want in C, effectively you have to write an
    >interpreter for another language.


    Not in standard C, that is true.

    OP should, of course, google for "avcall".
     
    Kenny McCormack, Jan 30, 2008
    #5
    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. Replies:
    10
    Views:
    635
    Torsten Bronger
    Dec 31, 2007
  2. grbgooglefan
    Replies:
    2
    Views:
    453
    Pascal Bourguignon
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    0
    Views:
    422
    grbgooglefan
    Jan 30, 2008
  4. Hicham Mouline
    Replies:
    1
    Views:
    369
    Vladyslav Lazarenko
    Mar 27, 2009
  5. Xeno Campanoli
    Replies:
    2
    Views:
    108
    Heesob Park
    Nov 4, 2008
Loading...

Share This Page