Thread: Can you execute a function from the name stored in a variable?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    58

    Post Can you execute a function from the name stored in a variable?

    Hi,

    I'd like to create a struct for a display function. The function should take in an array of structs like:

    Code:
    struct menu_item
    {
        int mode;
        char menu_display_name[100];
        char function_to_run[100];
    };
    Is it even possible to execute a function by the name stored in a string? Keep in mind, it doesn't have to be a string, if it is possible with any method, let me know.

    Ty.
    Last edited by Yonut; 05-03-2020 at 05:25 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    Yes, by mapping function names to function pointers. Actually, in this case you don't need the function name; just change the function_to_run member to be a function pointer. I am assuming that all these functions have the same parameter list and return type; if you want them to vary, then it would be more difficult.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    58
    Quote Originally Posted by laserlight View Post
    I am assuming that all these functions have the same parameter list and return type; if you want them to vary, then it would be more difficult.
    They do vary in type, but not in amount. It seems all the functions take in one arguement of varying types. Can I just use (void *) since the number of arguments doesn't change?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    If 'any' type is limited to some defined subset, you could use a union.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    59
    May be this example can help you:
    Code:
    #include <iostream>
    #include <math.h>
    
    struct tbl
        {
         std::string cmdtxt;
         double (*funki)(double a);
        };
    
    struct tbl tabelle[3] = {
        {"sin",      sin},
        {"cos",      cos},
        {"tan",      tan},
        };
    
    double bogenmass(double grad);
    
    
    int main (int argc , char **argv)
    {
    
     int i;
     
     double winkel = 30.000;
     
     std::cout << "Enter an angle > 0 and < 90 degree: ";
     std::cin >> winkel; 
     
       for(i = 0; i < 3; i++)
            std::cout << tabelle[i].cmdtxt << "(" << winkel<< ") = " << tabelle[i].funki(bogenmass(winkel)) << std::endl; 
       
      return 0;
    }
    
    // Wandelt grad in Bogenmass um
    double bogenmass(double grad)
    {
     double boma, pi = 3.141592653589793;
    
     boma = (grad * pi)  / 180;
     return boma;
    }

  6. #6
    Registered User
    Join Date
    May 2020
    Posts
    1
    $function_name = 'foobar';

    $function_name(arg1, arg2);

    call_user_func_array($function_name, array(arg1, arg2));

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    58
    Quote Originally Posted by rusyoldguy View Post
    May be this example can help you:
    Sorry, but C++ has never been able to help me with C.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,170
    > Sorry, but C++ has never been able to help me with C.
    Apart from the cin/cout, the rest is perfectly valid C.
    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.

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    58
    Quote Originally Posted by Salem View Post
    > Sorry, but C++ has never been able to help me with C.
    Apart from the cin/cout, the rest is perfectly valid C.
    Yeah, but in the beginning of me learning C on my own, a lot of people tried helping me with my programming problems by sending me links to C++ programs which, for the most part, wasted my time showing me techniques that weren't available to me in C. I wasted a lot of time trying to understand functions I couldn't use. So the moment I see someone reference C++ in their response, I usually just ignore it.

    I mean there IS a reason why I have never searched or posted in the C++ forum. I'm actually surprised at how many people feel they are the same language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2009, 03:43 PM
  2. variable stored in register or not ???
    By dhondi in forum C Programming
    Replies: 3
    Last Post: 11-03-2009, 01:03 PM
  3. Can scanf read from a stored variable?
    By azrael in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 03:54 AM
  4. Replies: 1
    Last Post: 07-28-2003, 12:56 PM
  5. Passing a function name as a variable to execute it?
    By Zuul in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 12:42 PM

Tags for this Thread