Thread: How to declare this function...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    54

    How to declare this function...

    Declare a pointer to a function which takes array of pointer to a function as an argument and return the pointer to a function.....
    how to declare this function....

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You really ought to do your own homework...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    how to declare this function
    use typedef

    Code:
    typedef int (* fpointer)(int,char*);
    and use the new type as you need
    Code:
    fpointer func(fpointer f[], size_t arr_size);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Thanks vart.. I am not able to understand can you explain me in detail vart....

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by sreeramu View Post
    Thanks vart.. I am not able to understand can you explain me in detail vart....
    What exactly you do not understand? There are only 2 lines of code here...

    Do you know what typedef is?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I have used this example in the past for such posts. So this help you as well in explaining an example of using a function pointer and how to declare it, outside of main.


    http://www.cplusplus.com/doc/tutorial/pointers.html

    Code:
    int addition (int a, int b) {
      return (a+b); }
    
    int subtraction (int a, int b) {
      return (a-b);}
    
    int operation (int x, int y, int (*functocall)(int,int))
    {
      int g;
      g = (*functocall)(x,y);   //A pointer to a function type 4.12 and 1.34 of comp.lang.c FAQ.
      return (g);
    }
    
    int main ()
    {
      int m,n;
      int (*minus)(int,int) = subtraction;
    
      m = operation (7, 5, addition);
      n = operation (20, m, minus);
      printf("%d\n", n);
      return 0;
    }
    Note:

    A function call actually requires a function pointer value before the
    parentheses. In a typical "direct" function call, this pointer value
    is obtained from the name of the function.
    A function name (more generally, any expression of function type) is
    implicitly converted to a pointer to the function in most contexts.
    (The exceptions are when it's the operand of a sizeof operator, which
    is illegal rather than yielding a pointer size, and when it's the
    operand of a unary "&", which yields the address of the function.)

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Code:
    g = (*functocall)(x,y);
    could be just
    Code:
    g = functocall(x,y);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM