how to give typedef for a function pointer?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bajajv
    New Member
    • Jun 2007
    • 152

    how to give typedef for a function pointer?

    Hi,
    I want to typedef a function pointer, but getting some errors.

    This is the code:

    Code:
    #include <stdio.h>
    typedef ((int) (*) (int, int)) Callback;
    Callback cb; 
    
    int add(int i, int j) 
    {return (i+j);}
    
    int main()
    { cb = &add;
      int res;
      res = cb(10, 20);
      printf("%d", res);
      return 0;
    }
    How to give this typedef statement?
    Last edited by Banfa; Jul 8 '10, 12:34 PM. Reason: Added [code]...[/code] tags please use them in future
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The thing to remember with typedefs is the type name goes exactly where the variable name goes if you where creating a variable of that type.

    Of course that doesn't help if you don't know how to create pointer to function variable. What you are actually creating is a pointer, the return type and parameter list just modify the pointer so the variable (or type name) go next to the *.

    typedef int (*Callback)(int , int);

    Comment

    • bajajv
      New Member
      • Jun 2007
      • 152

      #3
      Got the point.. thanks.. :)

      Comment

      Working...