Function calls using variables

Discussion in 'C Programming' started by simonjpaulger@hotmail.com, May 30, 2005.

  1. Guest

    In PHP (and other languages too, im not sure), you can call a function
    using a variable itself, the variables string contains the function
    name, which in turn runs.

    In my situation, i have an xml file with a common descriptive name, and
    a function name. I want to be able to parse the xml file and call the
    function name defined in the xml, just like php can call functions with
    variables. I've checked both The C Programming Language by K&R and the
    internet in general but can not find anything, but im not sure im
    looking with the right terminology.

    Is it possible in C to call functions using variable strings?

    Can anyone point me in the right direction?

    Many thanks,
    Brilte
     
    , May 30, 2005
    #1
    1. Advertising

  2. bjrnove Guest

    Hi.

    No, not really. You could make something similar, but nothing really
    generic.

    #define MAX_FUNC_NAME_LENGHT 64

    /* All the functions vould have to have this initializer */
    typedef int(MYSTDFUNC)(void*)

    /* This struct will store the functions */
    typedef struct tagFUNCTION
    {
    char szName[MAX_FUNC_NAME_LENGHT];
    MYSTDFUNC func;
    } FUNCTION, *LPFUNCTION;

    /* Create an array of all the functions */
    FUNCTION functions[] = {
    {"Func1", Func1},
    {"Func2", Func2}
    };

    Now all you would have to do is search trough the array of functions
    until the name maches.

    The problem with this way of doing things would be that the declaration
    would have to be the same for all the functions. This is why I chose
    void*, so that I f.eg could pass a struct with whatever arguments I
    need.

    --
    bjrnove
     
    bjrnove, May 30, 2005
    #2
    1. Advertising

  3. > Is it possible in C to call functions using variable strings?

    Not really. In C all of the names get stripped off during compilation.
    All that's left are the addresses of the functions in memory.
    However, if you create a dynamic library, the names of the exported
    library functions are usually encoded in the library. The way to access
    these functions vary by platform, but you can, for example, check out
    dlopen/dlsym on Linux.

    Jon
    ----
    Learn to program using Linux assembly language
    http://www.cafeshops.com/bartlettpublish.8640017
     
    Jonathan Bartlett, May 30, 2005
    #3
  4. Eric Sosman Guest

    wrote:
    > In PHP (and other languages too, im not sure), you can call a function
    > using a variable itself, the variables string contains the function
    > name, which in turn runs.
    > [...]
    > Is it possible in C to call functions using variable strings?
    >
    > Can anyone point me in the right direction?


    This is Question 20.6 in the comp.lang.c Frequently
    Asked Questions (FAQ) list

    http://www.eskimo.com/~scs/C-faq/top.html

    --
    Eric Sosman
    lid
     
    Eric Sosman, May 30, 2005
    #4
  5. Malcolm Guest

    <> wrote
    >
    > Is it possible in C to call functions using variable strings?
    >
    > Can anyone point me in the right direction?
    >

    You best bet is probably to write the function in C, and then use some
    existing system that implements dynamic binding by name at runtime, and can
    interface to libraries with C linkage.

    To implement call by name in C, you would have to have a list of names and
    function pointers, and then retrive the a function pointer to the function.
    If you don't know the parameters the function takes at compile time, it is
    virtually impossible to build an argument list using C, though it can be
    done in assembler.

    It is so clumsy that you'd have to have a really good reason for attempting
    this. If you want to call functions by name, C isn't the language to do it
    in.
     
    Malcolm, May 30, 2005
    #5
  6. "Malcolm" <> writes:
    > <> wrote
    >>
    >> Is it possible in C to call functions using variable strings?
    >>
    >> Can anyone point me in the right direction?
    >>

    > You best bet is probably to write the function in C, and then use some
    > existing system that implements dynamic binding by name at runtime, and can
    > interface to libraries with C linkage.
    >
    > To implement call by name in C, you would have to have a list of names and
    > function pointers, and then retrive the a function pointer to the function.
    > If you don't know the parameters the function takes at compile time, it is
    > virtually impossible to build an argument list using C, though it can be
    > done in assembler.


    The phrase "call by name" usually refers to an argument passing
    convention in which argument expressions are not evaluated before the
    call. This goes back to Algol 60; C doesn't support it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 30, 2005
    #6
  7. Malcolm Guest

    "Keith Thompson" <> wrote
    >
    > The phrase "call by name" usually refers to an argument passing
    > convention in which argument expressions are not evaluated before the
    > call. This goes back to Algol 60; C doesn't support it.
    >

    Dynamic runtime binding by name, then.
     
    Malcolm, May 30, 2005
    #7
  8. In article <>,
    Eric Sosman <> wrote:
    > wrote:
    >> In PHP (and other languages too, im not sure), you can call a function
    >> using a variable itself, the variables string contains the function
    >> name, which in turn runs.
    >> [...]
    >> Is it possible in C to call functions using variable strings?
    >>
    >> Can anyone point me in the right direction?

    >
    > This is Question 20.6 in the comp.lang.c Frequently
    >Asked Questions (FAQ) list
    >
    > http://www.eskimo.com/~scs/C-faq/top.html


    (Which basically says the same thing as the first response here - build up
    a table mapping strings to function pointers and go from there)

    I'm surprised nobody has given the obvious and correct answer - which is:

    Ask in comp.unix.programmer and I'll tell you whatever you want to know.

    Note: It is actually quite easy (and fun!) to do this - on supported
    platforms.
     
    Kenny McCormack, May 31, 2005
    #8
    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. Honne Gowda A
    Replies:
    2
    Views:
    918
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    797
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Replies:
    2
    Views:
    972
    Bengt Richter
    Aug 1, 2005
  4. Replies:
    9
    Views:
    1,015
  5. Bob
    Replies:
    5
    Views:
    290
Loading...

Share This Page