general function calling

This is a discussion on general function calling within the C Programming forums, part of the General Programming Boards category; I was wondering if there was a way, using macros, to define functions in a very general way, based on ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    35

    general function calling

    I was wondering if there was a way, using macros, to define functions in a very general way, based on a common key that will call a function based on a specific key. Like this:
    There are a bunch of buttons, but I don't want to add an event signal to each, is there a way to sort of use a char title or other title and call the function based on the value of that buttons title? Thank you.
    you make me rery ascared

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    Try Function Pointers

    Gday,

    You may wish to have a look into function pointers. The basic gist is to define a variable (pointer) that can point to a function. The only catch is that when you declare the pointer, it must match the prototype of the function it will be told to point to. EXAMPLE:
    Code:
    #define MESSAGE "I think the people down the hall know who you are"
    
    int add(int a, int b);
    int sub(int a, int b);
    int Quote(int a, int b){ return MESSAGE; };
    
    //so these functions all have the same prototype right?
    //to make a pointer to them, do this:
    
    int (*FunctionPtr)        (int, int);//()needed to overide * precedence
    
    FunctionPtr = sub;
    
    int result = FunctionPtr(456 - 123);
    
    printf("The result was %d\n", result);         
    //that code calls the sub function, the same as if you used sub()
    Obvously, you would have a switch or something to assign the pointer to the function you wish to use, but you get the idea! And it's really that straight forward in C.
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    thank you! Just out of curiosity, is there anyway other way to do something like what I had in my previous post? I am definately going to use this, its almost exactly what I was looking for!
    you make me rery ascared

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You could use an array of function pointers, like so.

    Code:
    #include <stdio.h>
    
    int add(int a, int b)
    {
      return(a + b);
    };
    
    int sub(int a, int b)
    {
      return(a - b);
    };
    
    int (*fps[]) (int, int) = { add, sub };
    typedef enum { FP_ADD, FP_SUB, FP_COUNT } FPS;
    const char *const fps_str[FP_COUNT] = { "ADD", "SUB" };
    
    int main(void)
    {
      int i;
      int ans;
    
      for (i = 0; i < FP_COUNT; i++)
      {
        ans = fps[i](1, 2);
        printf("1 %s 2 = %d\n", fps_str[i], ans);
      }
    
      return(0);
    }
    
    /* 
     Program output:
    
    1 ADD 2 = 3
    1 SUB 2 = -1
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Here is another variation on the theme.
    Code:
    #include <stdio.h>
    
    typedef void (*fpvv)(void);
    
    void foo(void) { puts("foo"); }
    void bar(void) { puts("bar"); }
    
    fpvv event_handler(int button)
    {
       switch ( button )
       {
       case 4: return foo;
       case 6: return bar;
       default: break;
       }
       return NULL;
    }
    
    int main(void)
    {
       int i;
       for ( i = 0; i < 10; ++i )
       {
          fpvv function = event_handler(i);
          if ( function )
          {
             printf("i = %d: ", i);
             function();
          }
       }
       return 0;
    }
    
    /* my output
    i = 4: foo
    i = 6: bar
    */
    [EDIT]
    And as long as I'm messing around with this...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef void (*fpvv)(void);
    
    void up(void)    { puts(">> going up");    }
    void down(void)  { puts(">> going down");  }
    void left(void)  { puts(">> going left");  }
    void right(void) { puts(">> going right"); }
    
    fpvv event_handler(const char *text)
    {
       static const struct
       {
          const char  *label;
          const fpvv  function;
       } button[] =
       {
          { "up",    up    },
          { "down",  down  },
          { "left",  left  },
          { "right", right },
       };
       size_t i;
       for ( i = 0; i < sizeof button / sizeof *button; ++i )
       {
          if ( strcmp(button[i].label, text) == 0 )
          {
             return button[i].function;
          }
       }
       return NULL;
    }
    
    int main(void)
    {
       const char *message[] =
       {
          "hello","help","me","go","up","down","and","to","the",
          "left","or","right","please",
       };
       size_t i;
       for ( i = 0; i < sizeof message / sizeof *message; ++i )
       {
          fpvv function = event_handler(message[i]);
          printf("message[%2d] = %-6s : ", (int)i, message[i]);
          if ( function )
          {
             function();
          }
          else
          {
             puts("unknown");
          }
       }
       return 0;
    }
    
    /* my output
    message[ 0] = hello  : unknown
    message[ 1] = help   : unknown
    message[ 2] = me     : unknown
    message[ 3] = go     : unknown
    message[ 4] = up     : >> going up
    message[ 5] = down   : >> going down
    message[ 6] = and    : unknown
    message[ 7] = to     : unknown
    message[ 8] = the    : unknown
    message[ 9] = left   : >> going left
    message[10] = or     : unknown
    message[11] = right  : >> going right
    message[12] = please : unknown
    */
    [/EDIT]
    Last edited by Dave_Sinkula; 01-09-2004 at 11:23 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21