Thread: how write the syntax for function returing a pointer to a function?

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    how write the syntax for function returing a pointer to a function?

    Hi ,

    i know how to write a syntax for function returning a integer pointer --> int *fun(int,int)

    can any one suggest to how to write a syntax for function returning a integer pointer to a function taking arguments as two integer valules.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Create a typedef for the function pointer, then it all becomes dead easy
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The nice way to do it is to write out the function pointer type in a typedef, like

    typedef int (*foo)(void);

    Just like defining a function pointer, but foo is a name, so you can return a foo type.

  4. #4
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Thanks laserlight and whiteflags.

    Whiteflags, u mean

    int *fun(void) ---> function returns integer pointer?
    int (*foo)(void) --> function returning a integer pointer to a function taking no arguments?

    am i right?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    typedef int (*foo)(void);
    foo function(int, foo);
    foo bar;

    bar = function(42, somefunction);


    foo is a pointer to a function that returns an int and takes no arguments. bar is a variable of type foo. function is a function that takes an int argument and a foo argument, and returns a foo type.

    Then I gave you a line of code calling the function named function.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Quote Originally Posted by nkrao123@gmail. View Post
    Thanks laserlight and whiteflags.

    Whiteflags, u mean

    int *fun(void) ---> function returns integer pointer?
    int (*foo)(void) --> function returning a integer pointer to a function taking no arguments?

    am i right?
    Code:
    Try this one,
    typedef int (*foo)(void);    /* here, foo is a pointer to a function which taking no arguments and returning integer. */
    foo fun();                        /* here, fun is function which takes no arguments and returing foo(pointer to a function which taking no arguments and returning integer) */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a function pointer syntax
    By manasij7479 in forum C++ Programming
    Replies: 15
    Last Post: 07-04-2011, 11:08 AM
  2. Replies: 2
    Last Post: 12-01-2010, 01:05 PM
  3. Function syntax to return a pointer...
    By tzuch in forum C Programming
    Replies: 1
    Last Post: 05-29-2008, 07:53 AM
  4. struct pointer to function syntax
    By kermit in forum C Programming
    Replies: 2
    Last Post: 03-21-2004, 04:01 PM
  5. syntax to pass a member function pointer to another class?
    By reanimated in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2003, 05:24 PM