Thread: Function returning pointer to function

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    Function returning pointer to function

    Hello,

    I am getting a compilation error for this:

    Code:
    int(*)(int)fun();
    What I am trying to declare is a function that returns a pointer to a function taking an int argument and returning an int. Can anybody tell me where the problem is? Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DL1
    What I am trying to declare is a function that returns a pointer to a function taking an int argument and returning an int. Can anybody tell me where the problem is?
    When I get confused, I just "cheat" with a typedef:
    Code:
    typedef int (*Function)(int);
    
    Function fun();
    It makes the code more readable, anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It is easiest if you use a typedef:
    Code:
    typedef int (*fn)(int); // typedef fn as a function that takes an int and returns an int
    
    fn fun(); // Declare fun() which returns a function that takes an int and returns an int
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Thanks. That's what I ended up doing. I am still puzzled as to why my initial declaration didn't work though. I can't see any difference between it and the typedef version, other than that the latter introduces a new name.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So far as I can tell, the real version is this:
    Code:
    int (*(fun(void))) (int);
    What you originally posted is ... well, I'm pretty sure it's not even a little bit syntactically valid.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Nicely put. I see now where I went wrong, I think.

    And if the function took an argument of the same pointer type, it would presumably be declared as follows:

    Code:
    int (*(fun(int(*)(int)))) (int);
    Last edited by DL1; 08-07-2009 at 10:52 AM.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's correct.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07: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