Returning pointer to function from function

This is a discussion on Returning pointer to function from function within the C Programming forums, part of the General Programming Boards category; Hi, I am learning function pointers and I found it little bit confusing. I have wrote a program as shown ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    Returning pointer to function from function

    Hi,

    I am learning function pointers and I found it little bit confusing.

    I have wrote a program as shown below but it's not compiling.

    Please suggest me where I am wrong.

    Code:
    #include<stdio.h>
    
    int add( int a, int b) { return a+b; }
    
    (int(*)(int,int) display();
    
    void show( int(*)(int,int) );
    
    int main()
    {
    int(*fun)(int,int) = NULL;
    int(*fun)(int,int) = display();
    show( int(*fun)(int,int) );
    }
    
    int(*)(int)(int) display()
    {
     return &add;
    }
    
    void show( int (*ptr)(int a,int b) )
    {
    int result = (*ptr)(56,63);
    
    printf("%d",result) ;
    }
    Please let me know where I am wrong ?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Please let me know where I am wrong ?
    You have several syntax errors. Your compiler should have pointed out where they were.

    What you want to do is this:
    Code:
    int (*display())(int, int)
    {
        return add;
    }
    But honestly, it would be much simpler with a typedef:
    Code:
    typedef int (*display_function)(int, int);
    
    display_function display()
    {
        return add;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    Thanks for help!
    I have done it with typedef , but I want to do this by normal condition.
    Can you please tell me the prototype of display() function ?
    Compiler is giving error in prototype declaration...

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Can you please tell me the prototype of display() function ?
    I have shown you a possible implementation, so the prototype can be taken directly from that:
    Code:
    int (*display())(int, int);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    Thanks , Its working ....
    I have one question....What was the problem with this prototype I have declared previously ?

    int(*)(int,int) display();
    Thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    What was the problem with this prototype I have declared previously ?
    It is simply syntactically incorrect. Note that you still have other errors that need to be fixed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    I wrote that prototype since this,

    void show( int(*)(int,int) ); //THis was working fine
    Was working fine.I concluded (previously) that if we received the parameter like this then we can return it in the same fashion.

    Thanks

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Function pointer, array, and array pointers types wrap the declaration around the object. So declaring a local looks like this. Declaring a local int *provided for comparison:
    Code:
    int *intptr;
    int(*funcptr)(int,int);
    int array[10];
    int (*arrayptr)[10];
    Typedefs are the same:
    Code:
    typedef int *intptr_t;
    typedef int(*funcptr_t)(int,int);
    typedef int array_t[10];
    typedef int (*arrayptr_t)[10];
    Function arguments are the same, except that the name can be omitted.
    Casts are look like omitted names too (except you can't cast to an array). The identifier is not part of the cast, so the cast does not wrap around it:
    Code:
    (int *)intptr;
    (int(*)(int,int) ) funcptr;
    (int (*)[10]) arrayptr;
    Return types follow the same pattern except that the "name" is the rest of the function declaration. This looks weird, but follows the pattern (again, you can't return arrays):
    Code:
    int *func1(char param);
    int(*func2(char param))(int,int);
    int (*func3(char param))[10];
    Last edited by King Mir; 06-09-2008 at 02:45 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM
  4. Typecasting a void* to a function pointer
    By Procyon in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2004, 05:43 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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