list of pointers to function

D

DevarajA

I'd like to create a linked list or a simple array of pointers to
function, but I can't find out how. I'm trying to do it like this:

struct fn_pointer{
void *pt_; /*the address*/
char *specs_; /*return type and args*/
};

In specs_ every single character identifies the return type [0] and the
type of the arguments. specs_ is 0 terminated. The problem is: how can i
build back the correct pointer type?
 
G

Gromer

I'd like to create a linked list or a simple array of pointers to
function, but I can't find out how. I'm trying to do it like this:

char * (*fxn[N])(char *)
 
D

DevarajA

Gromer ha scritto:
I'd like to create a linked list or a simple array of pointers to
function, but I can't find out how. I'm trying to do it like this:


char * (*fxn[N])(char *)

The array of char was only used to know the types. Eg: "ifc" would have
meant that the pointer referred to a function returning (i)nt and
accepting a (f)loat and a (c)har. I know, it was a stupid idea. Anyway I
don't know at compile time the types and the number of pointers because
the function that uses the list is placed into a library. So my list (or
array?) should be able to keep pointers to every kind of function.
 
F

Flash Gordon

DevarajA said:
Gromer ha scritto:
I'd like to create a linked list or a simple array of pointers to
function, but I can't find out how. I'm trying to do it like this:

char * (*fxn[N])(char *)

The array of char was only used to know the types. Eg: "ifc" would have
meant that the pointer referred to a function returning (i)nt and
accepting a (f)loat and a (c)har. I know, it was a stupid idea. Anyway I
don't know at compile time the types and the number of pointers because
the function that uses the list is placed into a library. So my list (or
array?) should be able to keep pointers to every kind of function.

A function pointer of one type can be freely converted to a function
pointer of another type (the standard guarantees this) as long as they
are converted back to the correct type before calling the function. So
you could use any function type you wanted as a generic function type. I
would suggest providing a typedef for the function type you chose, such as:
typedef void (*genfunc)(void);
Then it is easy for the user of the library to cast a function pointer
to this function pointer type and also easy for you to use it in
defining arrays, structures, parameters, return values etc.
 
D

DevarajA

Flash Gordon ha scritto:
DevarajA said:
Gromer ha scritto:
I'd like to create a linked list or a simple array of pointers to
function, but I can't find out how. I'm trying to do it like this:


char * (*fxn[N])(char *)


The array of char was only used to know the types. Eg: "ifc" would
have meant that the pointer referred to a function returning (i)nt and
accepting a (f)loat and a (c)har. I know, it was a stupid idea. Anyway
I don't know at compile time the types and the number of pointers
because the function that uses the list is placed into a library. So
my list (or array?) should be able to keep pointers to every kind of
function.


A function pointer of one type can be freely converted to a function
pointer of another type (the standard guarantees this) as long as they
are converted back to the correct type before calling the function. So
you could use any function type you wanted as a generic function type. I
would suggest providing a typedef for the function type you chose, such as:
typedef void (*genfunc)(void);
Then it is easy for the user of the library to cast a function pointer
to this function pointer type and also easy for you to use it in
defining arrays, structures, parameters, return values etc.

I wanted to write an improved atexit() that stored a list of arguments
for each (variadic) function (I'm not sure it's useful, it's just to
learn something new). But I don't know how to save the argument list and
how to pass it to the function when called through the pointer.
 
S

SM Ryan

# I'd like to create a linked list or a simple array of pointers to
# function, but I can't find out how. I'm trying to do it like this:
#
# struct fn_pointer{
# void *pt_; /*the address*/

Don't. You can use void(*)(void) as generic function pointer,
but don't cast to the generic data pointer void*.

# char *specs_; /*return type and args*/
# };
#
# In specs_ every single character identifies the return type [0] and the
# type of the arguments. specs_ is 0 terminated. The problem is: how can i
# build back the correct pointer type?

You can't, in general, on standard C: it doesn't have dynamic types
or runtime function argument list construction. If all the return types
and arguments are known ahead of time, you can have a basic switch
statement to cast the function to the desired type and call it.

You can do with assembly language, and perhaps your compiler does
provide a way to do it, but it is not portable.
 

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