How to declare this function...

This is a discussion on How to declare this function... within the C Programming forums, part of the General Programming Boards category; Declare a pointer to a function which takes array of pointer to a function as an argument and return the ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,449
    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);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,449
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,449
    Code:
    g = (*functocall)(x,y);
    could be just
    Code:
    g = functocall(x,y);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 06: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, 01:46 PM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

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