typedef

J

j0mbolar

typedef void (*genfunptr)(void);
typedef genfunptr (*fdset_callback_t)(fdset_t);

fdset_callback_t accept_connection(fdset_t set) {
(void)set;
return accept_connection;
}

-> return from incompatible pointer type


why does this happen?
 
W

Walter Roberson

typedef void (*genfunptr)(void);
typedef genfunptr (*fdset_callback_t)(fdset_t);
fdset_callback_t accept_connection(fdset_t set) {
(void)set;
return accept_connection;
}
-> return from incompatible pointer type
why does this happen?

The result type for accept_connection is fdset_callback_t .
Referring to the typedef, that's a pointer to a function that takes
fdset_t as an argument and returns as a result a pointer to
a function that has no parameters and returns nothing.

In the function, you return accept_connection which is the
function itself. we know that mentioning a function by name usually
devolves into a pointer to that function, so we're starting right
with a pointer to a function. If we compare signatures, we see that
that pointer-to- function takes fdset_t as an argument, which
matches the argument type we need for the result value.

Now look at the result needed for the pointer-to-function.
fdset_callback_t needs the result of the function to be a pointer to a
function, which is good so far. But that function pointer returned
must, in accordance with genfunptr, be for a function that takes
no arguments -- and that doesn't match the result of accept_connection
because that -does- take a parameter (fdset_t).


You "should" have infinite recursion on the result type for
accept_connection, but you can't build that infinite recursion.
What might perhaps work instead is

typedef void* (*genfunptr)();

That is a function pointer with unspecified arguments that
returns a pointer to -something-. You should probably do
a typecast on the return value of accept_connection to
get it into the theoretically-correct type.
 
E

Emmanuel Delahaye

j0mbolar wrote on 24/09/05 :
typedef void (*genfunptr)(void);
typedef genfunptr (*fdset_callback_t)(fdset_t);

fdset_callback_t accept_connection(fdset_t set) {
(void)set;
return accept_connection;
}

-> return from incompatible pointer type

why does this happen?

It happens exactly what did the message said. The types are
incompatible.

You claim to return the address of a function with the

genfunptr f (fdset_t);

signature

and actually, you return a function with the

fdset_callback_t accept_connection(fdset_t set);

signature, that are different, because 'genfunptr'is not the same type
than 'fdset_callback_t'

I'm not sure there is a way for a function to return it's own signature
(kidna recursive declaration...)

BTW, why do you need this ? If you have a access to the function's name
and prototype, use it directly. What do you intend to do exactly ?


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
E

Emmanuel Delahaye

Walter Roberson wrote on 24/09/05 :
typedef void* (*genfunptr)();

That is a function pointer with unspecified arguments that
returns a pointer to -something-. You should probably do
a typecast on the return value of accept_connection to
get it into the theoretically-correct type.

This 'something' is the address of an object (or NULL). It's not the
address of a function. From a portable-C point of view, the types are
not compatible.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
C

Chris Torek

I'm not sure there is a way for a function to return it's own signature
(kidna recursive declaration...)

I thought this was in the FAQ but could not find it.

To define a function whose return type is "pointer to function
returning <self>" requires going through a struct or union. For
instance:

struct state {
struct state (*next)(void);
};
struct state state0(void);
struct state state1(void);
struct state state2(void);

void f(void) {
struct state cur = { state0 };

while (cur.next != NULL)
cur = cur.next();
}

struct state state0(void) {
struct state next = { state1 };

puts("state0");
/* optionally, set next.next to state2 or NULL */
return next;
}

/* and so on */
BTW, why do you need this ? If you have a access to the function's name
and prototype, use it directly. What do you intend to do exactly ?

The usual application for this is some form of state machine (as
shown above).

Of course, one would more typically also provide some argument(s)
to each state-function, and perhaps also have the state-function
modify the current state (which would still contain the "next state"
function-pointer) instead of returning a new "struct state".
 
B

Ben Pfaff

Chris Torek said:
I thought this was in the FAQ but could not find it.

1.22: How can I declare a function that can return a pointer to a
function of the same type? I'm building a state machine with
one function for each state, each of which returns a pointer to
the function for the next state. But I can't find a way to
declare the functions.

A: You can't quite do it directly. Either have the function return
a generic function pointer, with some judicious casts to adjust
the types as the pointers are passed around; or have it return a
structure containing only a pointer to a function returning that
structure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top