Results 1 to 4 of 4

Thread: Help needed calling C function using variable name

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Help needed calling C function using variable name

    Hi All,
    I have 100 functions which have similar name but different in last few characters. for example, void get_fun1(void), void get_fun2(void), void get_fun3(void), ..........etc void get_fun100(void).


    Now i want to call those functions with a single variable dynamically. I can say like, i will get some number from some other module. If i get the value as 3 from that other module, i should call that function as get_fun3().

    So i need a generic "function calling" statement to invoke any one of the function out of hundred.

    i came to know that there is a methodology like .....


    Code:
    #define get(x) get_##x()
    void get_1()
    {
    printf("\nOne");
    }
    void get_2()
    {
    printf("\nTwo");
    }
    void main()
    {
    int i;
    scanf("%d",&i);
    get(1);
    }

    If i pass the static values (1,5,8...) to the get() function in the above code, it is working good. Suppose if i pass the value as a variables (i), it is not working.

    Can any one suggest me how to solve this issue?

    Thanks in advance,
    Raghunadhs

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Help needed calling C function using variable name

    Just use an array of function pointers. That way you needn't mess around with all that messy scanf() stuff...

    Code:
    #include "stdio.h"
    
    void func_0()
    {
      printf("this is func_0()\n");
    }
    
    void func_1()
    {
      printf("this is func_1()\n");
    }
    
    void func_2()
    {
      printf("this is func_2()\n");
    }
    
    
    typedef void (*pfunc)(); //function pointer data type definition
    
    int main(int argc, char** argv)
    {
      //declare an array of function pointers (you can use macros for this if you like)
      pfunc functions[3] = { func_0, func_1, func_2 };
    
      //declare the integer that chooses what function gets called...
      int funcNumber = 0;
    
      functions[funcNumber++](); //0
      functions[funcNumber++](); //1
      functions[funcNumber++](); //2
    
    
      return 0;
    }
    I don't live here any more.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: Help needed calling C function using variable name

    Thank you very much wossname, it helped me a lot.

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Help needed calling C function using variable name

    You are most welcome.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width