return pointer to function? 
Author Message
 return pointer to function?

Hi folks-

news server seems to break.

I want to build a function that will take as input an integer, and based on
that integer, return a pointer to a function.  I gave up on trying to make
the function return value a pointer to a function, and instead tried the
"pass a pointer to a pointer in the parameters list" method, a la:

#include "checksubs_ser.h"
int Checksub_ser(
                 int checksub,
                 int (**pChecksub)()  /* pointer to a pointer to a
                                         function?*/
                 )
{
  int (*sub)();

  switch(checksub)
    {
    case 0:
      sub = CheckOneSerial;
      break;
    case 1:
      sub = CheckTwoSerial;
      break;
    default:
      /* not found! */
      return(0);
    }
  *pChecksub = sub;
  return(1);

Quote:
}

The two functions CheckOneSerial and CheckTwoSerial are declared in
checksubs_ser.h.  This does not work in the following way.  It compiles OK,
and this function runs OK, but when I call the function pointed to (either
CheckOneSerial or CheckTwoSerial), it says "illegal operation".

I call Checksub_ser() in this manner:

  i = Checksub_ser(
                   checkid,
                   &chksub
                   );  

where checkid is an integer (0 or 1), and chksub is declared as

int (*chksub)();

Thanks much!
Andy

P.S. For those who will ask the inevitable question:  why on earth do you
want to do this?  I want to be able to easily add more subroutines to the
list, in addition to CheckOneSerial and CheckTwoSerial.  Having this
function means I will minimize the code changes - I just have to add a case,
and add the function declaration in checksubs_ser.h.

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

Dept. of Pure and Applied Mathematics|                             |
Washington State University          | http://www.*-*-*.com/ ~felta/  |
-------------------------------------------------------------------
Be the person your dog thinks you are.
--



Sat, 02 Feb 2002 03:00:00 GMT  
 return pointer to function?

Quote:

> Hi folks-

> news server seems to break.

> I want to build a function that will take as input an integer, and based on
> that integer, return a pointer to a function.  I gave up on trying to make
> the function return value a pointer to a function, and instead tried the
> "pass a pointer to a pointer in the parameters list" method, a la:

Don't give up so quickly.

typedef int (*pChecksub)();
pChecksub Checksub_ser(int checksub);

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com
--



Sat, 02 Feb 2002 03:00:00 GMT  
 return pointer to function?

Quote:

>Hi folks-

>news server seems to break.

>I want to build a function that will take as input an integer, and based on
>that integer, return a pointer to a function.  I gave up on trying to make
>the function return value a pointer to a function, and instead tried the

You shouldn't have.  You could try something like

   extern int fn1(void);
   extern int fn2(void);

   int (*(get_fn(int select)))(void)
   {
      static int (*(afn[]))(void) = {fn1, fn2};

      if ( (select>=0) && (select < (sizeof(afn)/sizeof(afn[0]))) )
         return afn[select];

      return NULL;
   }

The definition can be read from the inside out like this: get_fn is a
function taking a single int parameter (named select) and returning a
pointer to a function taking no parameters and returning int.  Whew.
Got that?  ;-)

Quote:
>"pass a pointer to a pointer in the parameters list" method, a la:

>#include "checksubs_ser.h"
>int Checksub_ser(
>             int checksub,
>             int (**pChecksub)()  /* pointer to a pointer to a
>                                         function?*/

....returning int.  Yup.
[...snip code...]

Quote:
>The two functions CheckOneSerial and CheckTwoSerial are declared in
>checksubs_ser.h.  This does not work in the following way.  It compiles OK,
>and this function runs OK, but when I call the function pointed to (either
>CheckOneSerial or CheckTwoSerial), it says "illegal operation".

Looks OK to me.  Something else must be wrong.  I tried your code on
my system, replacing

   #include "checksubs_ser.h"

with

   #include <stdio.h>
   int CheckOneSerial(){puts("In One!");}
   int CheckTwoSerial(){puts("In Two!");}

and using the following main:

   int main (void)
   {
      int (*chksub)();
      int temp;

      temp = Checksub_ser(0, &chksub);
      if (temp)
         chksub();
      else
         puts("check 0 failed");

      temp = Checksub_ser(1, &chksub);
      if (temp)
         chksub();
      else
         puts("check 1 failed");

      return 0;
   }

I get

   D:\DAVE>chkser
   In One!
   In Two!

[...]

Quote:

>P.S. For those who will ask the inevitable question:  why on earth do you
>want to do this?  I want to be able to easily add more subroutines to the

Ours is not to reason why...  ;-)

Regards,

                          -=Dave
Just my (10-010) cents
I can barely speak for myself, so I certainly can't speak for B-Tree.
Change is inevitable.  Progress is not.
--



Sat, 02 Feb 2002 03:00:00 GMT  
 return pointer to function?

Quote:

>I want to build a function that will take as input an integer, and based on
>that integer, return a pointer to a function.  I gave up on trying to make
>the function return value a pointer to a function, and instead tried the
>"pass a pointer to a pointer in the parameters list" method, a la:
[...]
>The two functions CheckOneSerial and CheckTwoSerial are declared in
>checksubs_ser.h.  This does not work in the following way.  It compiles OK,
>and this function runs OK, but when I call the function pointed to (either
>CheckOneSerial or CheckTwoSerial), it says "illegal operation".

[...]

I've snipped all your original code, and put it into a little test
wrapper, below. This all runs okay, so the only potential problem is
that you're not calling chksub() correctly. Unfortunately, you omitted
the vital piece of code where that happens. :)

  #include <stdio.h>

  /* dummy functions */

  int CheckOneSerial(void) {
    return 1;
  }

  int CheckTwoSerial(void) {
    return 2;
  }

  int Checksub_ser(
           int checksub,
           int (**pChecksub)()  /* pointer to a pointer to a
                                           function?*/
           )
  {
    int (*sub)();

    switch(checksub)
      {
      case 0:
        sub = CheckOneSerial;
        break;
      case 1:
        sub = CheckTwoSerial;
        break;
      default:
        /* not found! */
        return(0);
      }
    *pChecksub = sub;
    return(1);
  }

  int main(void) {
    int i, checkid;
    int (*chksub)();

    checkid=0;
    i = Checksub_ser(checkid, &chksub);
    printf("%d\n", chksub());

    checkid=1;
    i = Checksub_ser(checkid, &chksub);
    printf("%d\n", chksub());

    return 0;
  }

If you want to change Checksub_ser so that it returns a pointer to a
function, you can do it as follows

  int (*Checksub_ser(int checksub))()
  {
    int (*sub)();

    switch(checksub)
      {
      case 0:
        sub = CheckOneSerial;
        break;
      case 1:
        sub = CheckTwoSerial;
        break;
      default:
        /* not found! */
        return(0);
      }
      return sub;
  }

Also, it would probably be a good idea to use full parameter lists

  int (*Checksub_ser(int checksub))(void)
  ...
    int (*sub)(void);
  ...
    int (*chksub)(void);

rather than the incomplete

  int (*Checksub_ser(int checksub))()
  ...
    int (*sub)();
  ...
    int (*chksub)();

which is is less type-safe (and obsolescent?)

-- Mat.
--



Sat, 02 Feb 2002 03:00:00 GMT  
 return pointer to function?

Quote:

> I want to build a function that will take as input an integer, and based on
> that integer, return a pointer to a function.  I gave up on trying to make
> the function return value a pointer to a function, and instead tried the
> "pass a pointer to a pointer in the parameters list" method, ...
>  when I call the function pointed to, it says "illegal operation".

You have to be careful when *using* pointers to functions, since
when you dereference it you don't have a function.  Since you didn't
show the actual invocation of the function, it's hard to debug.

To see how to declare a function returning a pointer to a function,
look at the declaration of signal() in <signal.h>.
--



Sat, 02 Feb 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

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

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

3. Function returning pointers to functions (repost)

4. Function returning pointers to functions

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

6. functions returning pointers to functions

7. QUESTION: functions returning pointers to functions

8. Return pointer to function as argument

9. Return pointer to function as arguement.

10. problem returning pointer from function

11. returning pointer from function

12. Returning pointers from functions ...

 

 
Powered by phpBB® Forum Software