Function returning pointers to functions 
Author Message
 Function returning pointers to functions

Hi,

I need a function returning a pointer to an other function. For some reason
it does not work correctly.

BTW, what is the return type of a pointer to a function ??   Maybe void * ,
as that is the largest address any pointer fits in , or is the a special
type for pointers to functions ??

Program code looks a bit like :

extern int f1();
extern int f2();

static (void *) getFunc(int i)
{
   switch i:
  {
       case 1:
          return $f1;
          break:
      case 2:
          return $f2;
          break:
   }

Quote:
}

int main(void)
{
      int  (* fP)();       <-- correct declaration ???
      fP = getFunc(1);  <-- get pointer to f1
      *fP();                  <-- call to f1

Quote:
}

Anyone who can tell me what is wrong in this code ?

Thanks a lot !

Anthony



Tue, 15 Feb 2005 15:53:37 GMT  
 Function returning pointers to functions

Quote:

> Hi,

> I need a function returning a pointer to an other function. For some reason
> it does not work correctly.

> BTW, what is the return type of a pointer to a function ??   Maybe void * ,
> as that is the largest address any pointer fits in , or is the a special
> type for pointers to functions ??

Pointers to functions need to be specific to the type of the function
that they are a pointer to.

Quote:

> Program code looks a bit like :

It'd be nice if you pasted exactly what it looked like, or at least a
simplified version that shows the problem, in future.

Quote:

> extern int f1();
> extern int f2();

> static (void *) getFunc(int i)

this should be:

static int (*getFunc(int i))()

Quote:
> {
>   switch i:
>  {
>       case 1:
>          return $f1;

huh? i think you mean

           return &f1;

Quote:
>          break:
>      case 2:
>          return $f2;

likewise here

Quote:
>          break:
>   }
> }

> int main(void)
> {
>      int  (* fP)();       <-- correct declaration ???

Yes.

Quote:
>      fP = getFunc(1);  <-- get pointer to f1
>      *fP();                  <-- call to f1

Just fP();

Quote:
> }

> Anyone who can tell me what is wrong in this code ?

> Thanks a lot !

> Anthony

        - Kevin.


Tue, 15 Feb 2005 16:27:22 GMT  
 Function returning pointers to functions

Quote:

> Hi,

> I need a function returning a pointer to an other function. For some reason
> it does not work correctly.

> BTW, what is the return type of a pointer to a function ??   Maybe void * ,
> as that is the largest address any pointer fits in , or is the a special
> type for pointers to functions ??

You can't use void * for a pointer to function, only for objects..

Quote:
> Program code looks a bit like :

> extern int f1();
> extern int f2();

> static (void *) getFunc(int i)

It's a bit weird to define functions that return pointers to function
without using typedefs, but you'd want:

static int ((*getFunc (int i))()) {

Easier to:

typedef int (*int_func)();

static int_func getFunc (int i) { ... }

Quote:
> {
>    switch i:

That ain't C --> switch (int_expression) { ... }

Quote:
>   {
>        case 1:
>           return $f1;

                   ^ Not allowed in ISO C
Quote:
>           break:

                 ^ s/:/;/
Quote:
>       case 2:
>           return $f2;

                   ^ Remove '$'
Quote:
>           break:

                 ^ s/:/;/
And add:
        default:
            return 0;

Quote:
>    }
> }

> int main(void)
> {
>       int  (* fP)();       <-- correct declaration ???

Yes.

Quote:
>       fP = getFunc(1);  <-- get pointer to f1

Yes.

Quote:
>       *fP();                  <-- call to f1

No.  Either:

        (*fP)();

or just:

         fP();



Tue, 15 Feb 2005 17:05:55 GMT  
 Function returning pointers to functions


Quote:
> I need a function returning a pointer to an other function. For some
> reason it does not work correctly.

Let's see the code...

Quote:
> BTW, what is the return type of a pointer to a function ??   Maybe

Depences on the function!

Quote:
> void * , as that is the largest address any pointer fits in , or is

void* is fine for objects. Certainely not (portably) for functions.

Quote:
> the a special type for pointers to functions ??

> Program code looks a bit like :

> extern int f1();
> extern int f2();

It appears that the type of the function is

typedef int func_f();

Quote:

> static (void *) getFunc(int i)

weird!

static func_f *getFunc(int i)

Quote:
> {
>    switch i:

Huh! Why not compiling the code before posting it...

    switch (i)

Quote:
>   {
>        case 1:
>           return $f1;

Assuming you meant & instead of $, it's useless.

           return f1;

Quote:
>           break:

           break;

Note that due to the return, this break is never reached.

Quote:
>       case 2:
>           return $f2;

ditto.

Quote:
>           break:

ditto.

Quote:
>    }

What if i is not 1 or 2 ?

Quote:
> }

> int main(void)
> {
>       int  (* fP)();       <-- correct declaration ???

Yes, or

   func_f *fP;

Quote:
>       fP = getFunc(1);  <-- get pointer to f1
>       *fP();                  <-- call to f1

or
        fP();

Note that comments are supposed to be wrapped by /*  */

If you meant that f1 and f2 have no parameters, say it:

extern int f1(void);
extern int f2(void);
typedef int func_f(void);

Note also that once the typedef is defined, the prototypes can be defined
as follow:

extern func_f f1;
extern func_f f2;

Quote:
> }

> Anyone who can tell me what is wrong in this code ?

The returned type was weird, not to mention the typos...

Try that:

#ifndef ED_FUN_H
#define ED_FUN_H

/* fun.h */

typedef int func_f (void);
func_f f1;
func_f f2;

#endif /* ED_FUN_H */
/* GUARD (c) AETA 2000 Dec 16 2000 Ver. 1.2 */

/* fun.c */

#include "fun.h"
#include <stdio.h>

int f1 (void)
{
   puts ("f1");
   return 0;

Quote:
}

int f2 (void)
{
   puts ("f2");
   return 0;

Quote:
}

/* main.c */

#include "fun.h"

#include <stddef.h>

static func_f *getFunc (int i)
{
   func_f *pf;

   switch (i)
   {
   case 1:
      pf = f1;
      break;

   case 2:
      pf = f2;
      break;

   default:
      pf = NULL;
   }
   return pf;

Quote:
}

int main (void)
{
   func_f *fP;
   int i;

   for (i = 0; i < 3; i++)
   {
      fP = getFunc (i);
      if (fP)
      {
         fP ();
      }
   }
   return 0;

Quote:
}

--
-ed- emdel at noos.fr
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
Albert Camus.


Tue, 15 Feb 2005 17:11:10 GMT  
 Function returning pointers to functions


Quote:
> Hi,

> I need a function returning a pointer to an other function. For some
reason
> it does not work correctly.

> BTW, what is the return type of a pointer to a function ??   Maybe void *
,
> as that is the largest address any pointer fits in , or is the a special
> type for pointers to functions ??

> Program code looks a bit like :

> extern int f1();
> extern int f2();

> static (void *) getFunc(int i)
> {
>    switch i:
>   {
>        case 1:
>           return $f1;
>           break:
>       case 2:
>           return $f2;
>           break:
>    }
> }

> int main(void)
> {
>       int  (* fP)();       <-- correct declaration ???
>       fP = getFunc(1);  <-- get pointer to f1
>       *fP();                  <-- call to f1
> }

> Anyone who can tell me what is wrong in this code ?

> Thanks a lot !

> Anthony

This is my best shot without rewriting the code:

#include <stdio.h>

typedef void (*pChecksub)(); /* Adapted from Pete Becker. */

static void f1()
{
   printf(" this is function f1 \n");

Quote:
}

static void f2()
{
   printf(" this is function f2 \n");

Quote:
}

static pChecksub getFunc(int i)
{
  switch (i)
  {
      case 1:
          return &f1;
          break;
      case 2:
          return &f2;
          break;
   }

Quote:
}

int main(void)
{
      void (*fP)();

      fP = getFunc(2);
      fP();
      return 0;

- Show quoted text -

Quote:
}



Tue, 15 Feb 2005 19:02:55 GMT  
 Function returning pointers to functions
On Fri, 30 Aug 2002 09:53:37 +0200, Anthony said:

Quote:
> I need a function returning a pointer to an other function. For
> some reason it does not work correctly.

Sure it does, you're just not doing it right.

return_type (*function_name(arg_list))(arg list);

So, for example, a function called foo taking a char as an
argument which returns a pointer to
a function which takes two integer arguments and returns int
would be declared as...
int (*foo(char))(int, int);

Quote:
> BTW, what is the return type of a pointer to a function ??

A pointer to a function points to a function. And functions can
return anything. So pointer to functions point at functions which
return anything.

Quote:
> Maybe void * ,
> as that is the largest address any pointer fits in , or is the a
> special type for pointers to functions ??

No - pointers to functions can usually look just like functions,
thankfully.

Here's a code snippet which might help...

#include <stdio.h>
#include <stdlib.h>

/* For ease of reading, typedef a function accepting 2 int
 * arguments and returning int
 */
typedef int foo(int a, int b);

/* Declare two functions which will do a particularly complicated
 * calculation
 */
foo add;
foo subtract;
/* We could also leave this as
 * int add(int a, int b);
 * int subtract(int a, int b);
 */

/* Define a function which accepts an int, and returns a pointer
 * to a function accepting 2 int arguments and returning int
 */
foo *grommit(int i)
{
  switch(i)
  {
    case 0:
      return add;
      break;
    case 1:
      return subtract;
      break;
    default:
      return NULL;
  }

Quote:
}

int main(void)
{
  foo *func; /* Declare pointer to our function type */

  func = grommit(0);
  printf("4 + 17 = %d\n", func(4, 17));
  func = grommit(1);
  printf("17 - 4 = %d\n", func(17, 4));

  return 0;

Quote:
}

int add(int a, int b)
{
  return a + b;

Quote:
}

int subtract(int a, int b)
{
  return a - b;

Quote:
}

Hope this helps,
Dave.

--
           David Neary,
     E-Mail: bolsh at gimp dot org
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html



Tue, 15 Feb 2005 20:35:55 GMT  
 Function returning pointers to functions

Quote:



> > Hi,

> > I need a function returning a pointer to an other function. For some
>  reason
> > it does not work correctly.

> > BTW, what is the return type of a pointer to a function ??   Maybe void *
>  ,
> > as that is the largest address any pointer fits in , or is the a special
> > type for pointers to functions ??

> > Program code looks a bit like :

> > extern int f1();
> > extern int f2();

> > static (void *) getFunc(int i)
> > {
> >    switch i:
> >   {
> >        case 1:
> >           return $f1;
> >           break:
> >       case 2:
> >           return $f2;
> >           break:
> >    }
> > }

> > int main(void)
> > {
> >       int  (* fP)();       <-- correct declaration ???
> >       fP = getFunc(1);  <-- get pointer to f1
> >       *fP();                  <-- call to f1
> > }

> > Anyone who can tell me what is wrong in this code ?

> > Thanks a lot !

> > Anthony

> This is my best shot without rewriting the code:

> #include <stdio.h>

> typedef void (*pChecksub)(); /* Adapted from Pete Becker. */

> static void f1()
> {
>    printf(" this is function f1 \n");
> }

> static void f2()
> {
>    printf(" this is function f2 \n");
> }

> static pChecksub getFunc(int i)
> {
>   switch (i)
>   {
>       case 1:
>           return &f1;
>           break;
>       case 2:
>           return &f2;
>           break;
>    }
> }

> int main(void)
> {
>       void (*fP)();

>       fP = getFunc(2);
>       fP();
>       return 0;
> }

Give this a try (this is done under GCC 3.1 compiler):

void
f1(int a)
{
   printf("F1: %d\n", a);

Quote:
}

void (*f2())(int a)  // Here's the trick, defining just like a function pointer
{
   printf("F2\n");
   return(f1);       // returns the f1 function

Quote:
}

int
main(int argc, char **argv)
{
   void (*f0)(int a);

   f0=f2();     // gets a pointer to the f1 function
   f0(111111);  // calls the f1 function

Quote:
}

The syntax is cool for f2()--

Dan



Wed, 16 Feb 2005 02:04:30 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Pointer to function returning pointer to function returning...

2. Function returning pointers to functions (repost)

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

4. functions returning pointers to functions

5. QUESTION: functions returning pointers to functions

6. How do I define function returning pointer to function returning int?

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

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

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

10. Declaring static function returning pointer to extern function

11. About function that returns function pointer

12. Function returning a function pointer.

 

 
Powered by phpBB® Forum Software