#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Call functions dinamically


    This code works, and the line number 13 is a comment of what I want, that is, calling a function from a string whose content is the same that the functions that I want to call.

    Thanks in advance,
    Joan Q

    #include <stdio.h>
    void execute_function( void (*function)() );
    void draw_pent( );

    int main(int argc, char *argv[])
    {

    char *func;
    func = "draw_pent";

    if( strcmp( func, "draw_pent" ) == 0 ) execute_function ( draw_pent );
    //what I want is something like:
    //execute_function ( func );

    }

    void execute_function( void (*function)() )
    {
    (*function)();
    }

    void draw_pent () {
    printf ("draw pent\n");
    }
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    You can use function pointers if you want to dynamically change what functions are executed. It is actually fairly straightforward, though the syntax can be a bit strange. Google is your friend here.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,393
    Rep Power
    1871
    > char *func;
    > func = "draw_pent";
    This would be
    Code:
        void (*func)();
        func = draw_pent;
    Please remember to use [code][/code] tags when posting code, so it looks like the above example (with line numbers, fixed width font and indentation).

    But if you're talking about resolving a function name string at run-time, then you'll need to use something like dlopen and dlsym
    Bear in mind that you don't really want the user to be able to type in any random function name and you to call it (possibly with incorrect parameters).

    A simple lookup table mapping char* names to function pointers is simple, safe and effective (and portable I might add - which dlopen et al are not).
    Last edited by salem; November 29th, 2012 at 06:54 AM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by joanillo
    This code works
    That's the way to do it, the basic way.

    You could create a hash table with the name of the function as key and the function pointer as value and use that instead of a series of strcmp().

    The series of strcmp() is easier to implement from scratch.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    If you're coding for a POSIX system (like Linux), you can also use dlfcn.h, which provides dynamic linking. This can be a convenient way to look up data (including functions) given a string. Note the pretty radical type casting, however, which is inherently dangerous. You must be absolutely sure that the function you resolve dynamically via dlsym really is a function and has the desired call signature.
    Code:
    /* Compile with gcc -ldl -rdynamic */
    
    #include <stdio.h>
    #include <dlfcn.h>
    #include <stdlib.h>
    
    void testit(void) {
        printf("Working!\n");
    }
    
    int main(void) {
        void *handle;
        void (*fn)(void);
        char *error;
    
        handle = dlopen(NULL, RTLD_LAZY);
        if (!handle) {
            fprintf(stderr, "%s\n", dlerror());
            exit(1);
        }
    
        dlerror();
    
        *(void **)(&fn) = dlsym(handle, "testit");
    
        if ((error = dlerror()) != NULL) {
            fprintf(stderr, "%s\n", error);
            exit(1);
        }
    
        fn();
    
        dlclose(handle);
        exit(0);
    }
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Thanks, it works! (though is really difficult to understand...)

IMN logo majestic logo threadwatch logo seochat tools logo