Function returning pointer to function

This is a discussion on Function returning pointer to function within the C++ Programming forums, part of the General Programming Boards category; Hello, I am getting a compilation error for this: Code: int(*)(int)fun(); What I am trying to declare is a function ...

  1. #1
    DL1
    DL1 is offline
    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
    21,659
    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.
    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
    Sep 2004
    Location
    California
    Posts
    3,246
    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
    DL1
    DL1 is offline
    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,185
    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
    DL1
    DL1 is offline
    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,246
    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, 08:39 PM
  4. Typecasting a void* to a function pointer
    By Procyon in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2004, 04:43 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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