QUESTION: functions returning pointers to functions 
Author Message
 QUESTION: functions returning pointers to functions

-----------------------------------------------------------------------------

Does anyone know anything about functions that return pointers to functions?

Suppose I want a function that takes an integer argument and returns a
pointer to a function that takes a string argument.  I discovered on my own,
after about thirty minutes of trial and error, that I could use a prototype
like the one below:

    double (*foo (int a)) (char *c);

where foo is the function that returns a pointer to the string-valued
function, where the referenced function returns a double.  The caller would
be as follows:

    foo (integer_argument) (string_argument);

I wrote some code to test something like this.  I got it working just fine
on a UNIX box and an MS-DOS box.  This program has no particular use, it
just demonstrates what I'm talking about.  What I am asking is this: does
anyone know whether or not this standard ANSI C?

----------------------------------------------------------- cut here --------
#include <stdio.h>

/*  You can put prototypes here if you want to.  */

double four (char *c) {
    printf ("integer argument is four; string is \"%s\"\n",c);
    return 4.0;
    }

double two (char *c) {
    printf ("integer argument is two; string is \"%s\"\n",c);
    return 2.0;
    }

double not_two_or_four (char *c) {
    printf ("integer argument is not two or four; string is \"%s\"\n",c);
    return 0.0;
    }

/*
 *  In this example, foo takes an integer argument and returns a
 *  pointer to a string-valued function.  The referenced string-valued
 *  function returns a double.
 */

double (*foo (int a)) (char *c)
    {
    if (a==4)
        return four;
    else if (a==2)
        return two;
    else
        return not_two_or_four;
    }

void main (void)
    {
    int a;
    char str[30];
    double (*fp)(char *c);
    double z;

    while ( scanf("%d %s",&a,str) == 2 ) /* read integer argument and
                                            string argument */
        {
        fp = foo(a);     /* pointer to int-valued function */
        (*fp)(str);      /* execute that function */

        z = foo(a)(str); /* this demonstrates the caller and also
                            uses the return value of the function
                            returned by foo. */

        printf ("Return value is: %20g\n",z);
        }
    }
----------------------------------------------------------- cut here --------

--
#!/bin/ksh

RANDOM}%10+2));until [ $c -eq 0 ];do s=$o$s;c=$((${c}-1));done;echo $h|sed "s/\
\(.\)/"$s"/g"|sed "s/^\(.*\)$/"$s"/g"|sed "s/\(.\)"`echo $s|sed "s/"'\\'$o"$//\
g"`"/"$o"/g"|sed "s/^\(.*\)"`echo $s|sed "s/"'\\'$o"$//g"`"$/"$o"/g"



Sun, 15 Oct 1995 23:27:19 GMT  
 QUESTION: functions returning pointers to functions

Quote:
>-----------------------------------------------------------------------------
>Does anyone know anything about functions that return pointers to functions?
>Suppose I want a function that takes an integer argument and returns a
>pointer to a function that takes a string argument.  I discovered on my own,
>after about thirty minutes of trial and error, that I could use a prototype
>like the one below:
>    double (*foo (int a)) (char *c);

FYI: there is a program called cdecl which can be ftp'd from somewhere (I
can't remember where, sorry).  I say to cdecl:

    declare foo as function(int) returning pointer to function(pointer to char) returning double

and cdecl says to me:

    double (*foo(int ))(char *)

Hope this helps.
--

3M Company                      phone:  (612) 737-3300
3M Center, Bldg. 235-3B-16      fax:    (612) 737-2700



Mon, 16 Oct 1995 23:04:44 GMT  
 QUESTION: functions returning pointers to functions

The easy way to define functions returning pointers to functions etc is
to to it in easy stages with typedef.

eg

    typedef char *foo(struct *really_big srb); // or even more complex

    typedef foo *baar(int i);    // Function returning pointer to ....

It's three years or so since I did this so please don't flame me for any
stupid mistakes in above. It should give you the idea, it works for any
complicated decl.

--
Regards

Alec Clews



Sat, 21 Oct 1995 19:19:29 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Question about signal()/pointers to functions that return pointers to functions

2. Pointer to function returning pointer to function returning...

3. a question on function returning a pointer to a function

4. How to use (pointer to function), and function and pointer to (pointer to function)

5. Function returning a function-pointer - how to prototype ?

6. Declaring static function returning pointer to extern function

7. About function that returns function pointer

8. Function returning pointers to functions (repost)

9. Function returning pointers to functions

10. (member) function returning pointer to functions like itself?

11. functions returning pointers to functions

12. Function returning a function pointer.

 

 
Powered by phpBB® Forum Software