Function returning pointers to functions (repost) 
Author Message
 Function returning pointers to functions (repost)

Hi,

Sorry guys for my latest code about functions returning pointers to
functions full with syntax errors.

I have repaired those syntax errors, and after some changes my code now
looks like:

<----------------- this is line #1
static void f1()
{
   printf(" this is function f1 \n");

Quote:
}

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

Quote:
}

static void (*getFunc(int i))
{
   switch (i)
  {
      case 1:
          return &f1;  <----------------- this is line #17
          break;
      case 2:
          return &f2; <----------------- this is line #20
          break;
   }

Quote:
}

int main(void)
{
      void (*fP)();
      fP = getFunc(2);  <----------------- this is line #28
      fP();

Quote:
}

and it does compile, but anyone who can explain what those warnings mean ?

"test.c", line 17: warning: return value type mismatch
"test.c", line 20: warning: return value type mismatch
"test.c", line 28: warning: assignment type mismatch:
        pointer to function() returning void "=" pointer to void

It even runs as expected,
[~/tst] sunray : cc  test.c
[~/tst] sunray : a.out
 this is function f2

It would be nice to understand what those warnings refer to in order to
implement a much more complicated function of that type.

BTW,  what does the line of code :
    static void (*getFunc(int i))
mean ?
Is that a function returning a pointer to a void and expecting an integer as
argument, or a pointer to a function expecting an integer as argument and
returning a normal void (no address).
And is that the same as :
   static (void *) getFunc(int i)
and if  not: what does this declaration mean ?

Thanks a lot !
Anthony



Tue, 15 Feb 2005 18:06:38 GMT  
 Function returning pointers to functions (repost)
Quote:
> static void f2()
> {
>    printf(" this is function f2 \n");
> }

> static void (*getFunc(int i))
> {
>    switch (i)
>   {
>       case 1:
>           return &f1;  <----------------- this is line #17

The function about to return here has been declared void
(nothing returned)

Quote:
>           break;
>       case 2:
>           return &f2; <----------------- this is line #20

Do.

Quote:
>           break;
>    }
> }

> int main(void)
> {
>       void (*fP)();
>       fP = getFunc(2);  <----------------- this is line #28

Since the function you're calling has been declared void,
you are better off not assigning anything to the "return value"

HINT K&R2: 5.11 Pointers to functions



Tue, 15 Feb 2005 19:04:40 GMT  
 Function returning pointers to functions (repost)


Quote:
> Sorry guys for my latest code about functions returning pointers to
> functions full with syntax errors.

> I have repaired those syntax errors, and after some changes my code
> now looks like:

> <----------------- this is line #1
> static void f1()
> {
>    printf(" this is function f1 \n");
> }

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

> static void (*getFunc(int i))

No. Please take some time to read the answers you have already received.

Quote:
> BTW,  what does the line of code :
>     static void (*getFunc(int i))
> mean ?

It's weird. I'm not sure it means anything...

Quote:
> Is that a function returning a pointer to a void and expecting an
> integer as argument, or a pointer to a function expecting an integer
> as argument and returning a normal void (no address).
> And is that the same as :
>    static (void *) getFunc(int i)
> and if  not: what does this declaration mean ?

As told before, using pointers to functions is much easier with typedefs.

--
-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 21:47:44 GMT  
 Function returning pointers to functions (repost)

Quote:

> Hi,

> Sorry guys for my latest code about functions returning pointers to
> functions full with syntax errors.

> I have repaired those syntax errors, and after some changes my code now
> looks like:

> <----------------- this is line #1
> static void f1()

Ok but, I prefer full prototypes:
   static void f1(void)

Quote:
> {
>    printf(" this is function f1 \n");
> }

> static void f2()

Ok but, I prefer full prototypes:
  static void f2(void)

Quote:
> {
>    printf(" this is function f2 \n");
> }

> static void (*getFunc(int i))

This defines a function returning a void pointer which
it seems by C99 standard is compatible only with object
pointers -- not function pointers. I think you are realy
looking for:

   static void (*getfunc(int i))(void)

Which (I hope) turns it into a function returning a pointer to
a function with an empty argument list and returning nothing (void).

Quote:
> {
>    switch (i)
>   {
>       case 1:
>           return &f1;  <----------------- this is line #17
>           break;
>       case 2:
>           return &f2; <----------------- this is line #20
>           break;
>    }

Usually considered undesirable to leave a random result
if the switch expresion is not matched. In this case
the function runs off the end without a return value.
Some compilers will issue a warning.

Quote:
> }

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

The full prototype would be:
         void (*fP)(void);

Quote:
>       fP = getFunc(2);  <----------------- this is line #28
>       fP();
> }

> and it does compile, but anyone who can explain what those warnings mean ?

> "test.c", line 17: warning: return value type mismatch
> "test.c", line 20: warning: return value type mismatch
> "test.c", line 28: warning: assignment type mismatch:
>         pointer to function() returning void "=" pointer to void

> It even runs as expected,
> [~/tst] sunray : cc  test.c
> [~/tst] sunray : a.out
>  this is function f2

> It would be nice to understand what those warnings refer to in order to
> implement a much more complicated function of that type.

> BTW,  what does the line of code :
>     static void (*getFunc(int i))
> mean ?
> Is that a function returning a pointer to a void and expecting an integer as
> argument, or a pointer to a function expecting an integer as argument and
> returning a normal void (no address).
> And is that the same as :
>    static (void *) getFunc(int i)

I believe it is equivalent to:
      static void getFunc(int i)
If you have copied the line from somewhere I suspect you've lost a pair
of parentheses:
      static void (*getFunc(int i))()

Malcolm Kay



Tue, 15 Feb 2005 22:28:13 GMT  
 Function returning pointers to functions (repost)

Quote:

> > BTW,  what does the line of code :
> >     static void (*getFunc(int i))
> > mean ?

Emmanuel Delahaye replied:

Quote:
> It's weird. I'm not sure it means anything...

It means "declare getFunc as function (argument i as int), returning
pointer to void". The outer parentheses are unnecessary.

Quote:
> > Is that a function returning a pointer to a void and expecting an
> > integer as argument,

Yes.

Quote:
> > or a pointer to a function expecting an integer
> > as argument and returning a normal void (no address).

No. To declare getFunc as pointer to function (expecting one int argument)
and returning void, you'd do this:
  void (*getFunc)(int);

Quote:
> > And is that the same as :
> >    static (void *) getFunc(int i)
> > and if  not: what does this declaration mean ?

No, this one is a syntax error as far as I can tell.

Perhaps what you really want is a
  function expecting [int] returning [pointer to function expecting [void]
returning [void]]

void f1(void) {puts("Function 1");}
void f2(void) {puts("Function 2");}

void (*getFunc(int i))(void)
{
  void (*p)(void);
  if(i == 1)
  {
    p = f1;
  }
  else
  {
    p = f2;
  }
  return p;

Quote:
}

--
Simon.


Wed, 16 Feb 2005 01:25:45 GMT  
 Function returning pointers to functions (repost)

Quote:

> Hi,

> Sorry guys for my latest code about functions returning pointers to
> functions full with syntax errors.

> I have repaired those syntax errors, and after some changes my code now
> looks like:

> <----------------- this is line #1
> static void f1()
> {
>    printf(" this is function f1 \n");
> }

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

/*
** I couldn't help but wonder if an array of function pointers
** might be useful to you.
*/

#include <stdio.h>

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

Quote:
}

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

Quote:
}

static void f0(void)
{
   printf(" this is neither function f1 nor f2 \n");

Quote:
}

int main(void)
{
    int index;
    static void (*array[])(void) = {f0, f1, f2};

    index = sizeof array / sizeof *array;
    while (index--) {
        array[index]();
    }
    return 0;

Quote:
}

--
 pete


Wed, 16 Feb 2005 08:08:37 GMT  
 
 [ 6 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. How to use (pointer to function), and function and pointer to (pointer to function)

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

5. Declaring static function returning pointer to extern function

6. About function that returns function pointer

7. Function returning pointers to functions

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

9. functions returning pointers to functions

10. Function returning a function pointer.

11. Function returning pointer to a function

12. Function pointer as a return value of a function

 

 
Powered by phpBB® Forum Software