Thread: how to call a function when I do not know its name directly

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    how to call a function when I do not know its name directly

    Hi. There is a function. Its name is stored perfectly including the brackets () just after it in a string variable. I want to call the function. How can I do that?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void function7(void);
    void function9(void);
    
    
    int main()
    {
        char* s = concat("function", "7");
        s=concat(s, "()");
    
        //here I want to call the function stored in the string s
    
        return 0;
    }
    
    
    void function7(void)
    {
        printf("Today is Monday.");
    }
    
    
    void function9(void)
    {
        printf("Today is Friday.");
    }
    Last edited by nerio; 01-12-2016 at 03:37 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    That is something that scripting languages can often do (evaluate newly minted code). Your basically asking to run a string as if it were C code. That's not really possible in C since C code generally needs to be compiled. You may be able to use function pointers to do what you want.
    Code:
    #include <stdio.h>
    
    typedef void Function(void);   // defines a function type named Function (takes void, returns void).
    
    void func0(void) {
      printf("func0 called\n");
    }
    
    void func1(void) {
      printf("func1 called\n");
    }
    
    int main() {
      Function *funcs[] = {
        func0,
        func1
      };
      int func_num = 0;
    
      funcs[func_num]();
      func_num++;
      funcs[func_num]();
    
      return 0;
    }

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    What on earth do you need to do that for?

    You can extend algorism's example so that you can refer to each function using its name, for example
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    typedef void function_t(void);
    
    void func1(void) { printf("func1() called\n"); }
    void func2(void) { printf("func2() called\n"); }
    void func7(void) { printf("func7() called\n"); }
    
    static const struct {
        const char *const name;
        function_t *const func;
    } functab[] = {
        { "func1", func1 },
        { "func2", func2 },
        { "func7", func7 },
    };
    #define FUNCTAB_SIZE (sizeof functab / sizeof functab[0])
    
    
    static function_t *find_function(const char *const name)
    {
        size_t i = FUNCTAB_SIZE;
        while (i-->0)
            if (!strcmp(functab[i].name, name))
                return functab[i].func;
        return NULL;
    }
    
    
    int main(int argc, char *argv[])
    {
        int  arg;
    
        if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
            size_t i;
            fprintf(stderr, "\n");
            fprintf(stderr, "Usage: %s [ -h | --help ]\n", argv[0]);
            fprintf(stderr, "       %s function ...\n", argv[0]);
            fprintf(stderr, "\n");
            fprintf(stderr, "Recognized functions:\n");
            for (i = 0; i < FUNCTAB_SIZE; i++)
                printf("\t%s\n", functab[i].name);
            fprintf(stderr, "\n");
            return EXIT_FAILURE;
        }
    
        for (arg = 1; arg < argc; arg++) {
            function_t *const func = find_function(argv[arg]);
            if (!func) {
                fprintf(stderr, "%s: No such function.\n", argv[arg]);
                return EXIT_FAILURE;
            }
            func();
            fflush(stderr);
        }
    
        return EXIT_SUCCESS;
    }
    and POSIX.1-2001 even provides dlopen() and dlsym() (part of the dynamic loader, so you'll need to link against it by adding -ldl at compile time) that can be used to examine the symbols in the program and even load new code (plugins) to the current program. I'm sure Windows has something similar, too.

    None of these specify what the function parameters are, and what the function returns, if anything. Those are things the program has to know in order to call the function, as there just is no way to find them out at run time. (Above, you could add a field, say type, to the functab, to identify which kind of function (parameters and return type if any) the function refers to.)

    None of those help with algebraic expressions like 2*x*sin(x*M_PI/180.0). For those, you need an algebraic expression parser -- basically, a (non-simple) calculator. It is a relatively common advanced exercise.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Quote Originally Posted by Nominal Animal View Post
    What on earth do you need to do that for?
    I actually need it for something I do on Javascript, but since I have noticed that most of Javascript's syntax is derived from C, I created a simple example of that in C and I asked it here.

    For my problem I have already found a solution without having to use the calling of a function that is inside a string.

    Here are some links online:


    call function from a string
    Call a function named in a string variable in C - Stack Overflow
    c++ - How to call a c function from name , which is stored in a char* pointer? - Stack Overflow
    reflection - Java: How to call a function whose name is stored in a string variable - Stack Overflow

    but in C# it is possible
    https://bytes.com/topic/c-sharp/answ...-stored-string

    Thank you for your posts, I will spend more time trying to understand what you wrote in detail.

    Intellectually I like the problem. It sounds like a nice challenge.
    Last edited by nerio; 01-12-2016 at 07:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call Overhead and Function Call Stack
    By Alam Khan in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2014, 08:28 AM
  2. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  3. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  4. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  5. Replies: 2
    Last Post: 06-21-2005, 02:41 PM