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

This is a discussion on how write the syntax for function returing a pointer to a function? within the C Programming forums, part of the General Programming Boards category; Hi , i know how to write a syntax for function returning a integer pointer --> int *fun(int,int) can any ...

  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
    22,265
    Create a typedef for the function pointer, then it all becomes dead easy
    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 whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    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
    66
    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, 12:08 PM
  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, 08: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

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