Must a function pointer have a prototype in C

M

mireillen

I did:

void * ( * function[FUNC_MAX] ) ()

Because I want an array with multiple function pointers that don' t
have the same prototype

but the senior programmer says it has to be

void * ( * function[FUNC_MAX] ) (void)

Should i do as he says and, for instance, cast my function pointers
when i assign/use them?
(so i dont get: "assignment from incompatible pointer type")
 
J

jacob navia

I did:

void * ( * function[FUNC_MAX] ) ()

Because I want an array with multiple function pointers that don' t
have the same prototype

but the senior programmer says it has to be

void * ( * function[FUNC_MAX] ) (void)

Should i do as he says and, for instance, cast my function pointers
when i assign/use them?
(so i dont get: "assignment from incompatible pointer type")

If you never make a mistake and you are 100% sure you will never make
it when using those function pointers it is OK. You do not need the casts.

If you *could* make a mistake it is better to have the compiler check
the call. Use the prototypes.
 
G

Guest

I did:

void * ( * function[FUNC_MAX] ) ()

Because I want an array with multiple function pointers that don' t
have the same prototype

I must admit mixing function pointers with arrays in the
same declaration makes my head hurt. This is a place where
typedefs can help.

typedef void* (*Fptr)(); /* ptr-to-function returning void* */

Fptr function [FUNC_MAX];

which I find a little easier to read.

If your array contains pointers to functions that don't
have the same prototype how do you know how to call them?
but the senior programmer says it has to be

void * ( * function[FUNC_MAX] ) (void)

I must confess I'd prefer the extra void, but I'd also
be concerned about exactly how readable stuff like this is.
Without checking I don't know if that's a array-of-ptr-to-function
or a erm... ok function-returning-ptr-to-array doesn't
really work. But I don't find it very readable
Should i do as he says and, for instance, cast my function pointers
when i assign/use them?
(so i dont get: "assignment from incompatible pointer type")

I'd need to know more about your program. What are these functions
used for?
 
M

mireillen

I'd need to know more about your program. What are these functions
used for?

The functions are mainly used for getting and setting parameters in a
struct (size, position, text)
typedef void* (*Fptr)(); /* ptr-to-function returning void* */
Fptr function [FUNC_MAX];

Thx i like the typedef it makes it way more readable.
 
B

Ben Bacarisse

I'd need to know more about your program. What are these functions
used for?

The functions are mainly used for getting and setting parameters in a
struct (size, position, text)
typedef void* (*Fptr)(); /* ptr-to-function returning void* */
Fptr function [FUNC_MAX];

Thx i like the typedef it makes it way more readable.

I have a slight preference for writing it this way:

typedef void *Function();

Function *function[FUNC_MAX];

so that you can see you have an array of pointer. Of course, what
else could it be with functions, but I prefer not to hide the pointer
in the typedef (unless there is good reason).

As to your original question... Omitting the void in the function
type is more descriptive since it means "an unspecified number of
parameters" where (void) means "no parameters". However, if all the
functions actually have the same type then you should use whatever
that type really is.

If the functions are of different types, then you have to convert the
pointers at the time of the function call and it does not matter
(except for readability) what type the pointer has to start with. A
function pointer can be convert to any other function pointer type.

The key issue here is what do the calls look like? If they all
involve a conversion (usually a cast) to a function pointer type that
matches the definition of the function being called, then all is well
and type you use above hardly matters. If the calls don't involve a
conversion, then the function definitions must match that of the
pointer. Calling functions /defined/ using the old non-prototype
syntax is fraught with subtle problems, so maybe that is what your
senior programmer is trying to avoid.
 
K

Keith Thompson

Ben Bacarisse said:
I have a slight preference for writing it this way:

typedef void *Function();

Function *function[FUNC_MAX];

You declared function as an array of pointers to pointers to
functions. I think you meant:

typedef void Function();

Function *function[FUNC_MAX];
 
B

Ben Bacarisse

Keith Thompson said:
Ben Bacarisse said:
I have a slight preference for writing it this way:

typedef void *Function();

Function *function[FUNC_MAX];

You declared function as an array of pointers to pointers to
functions.

I don't think so. The OP's example functions return void * so I
copied that:

| typedef void* (*Fptr)(); /* ptr-to-function returning void* */

but I took the pointer-to-function out of the typedef.
I think you meant:

typedef void Function();

Function *function[FUNC_MAX];

All this does is change the return type from void * to void, does it
not?
 
K

Keith Thompson

Ben Bacarisse said:
Keith Thompson said:
Ben Bacarisse said:
I have a slight preference for writing it this way:

typedef void *Function();

Function *function[FUNC_MAX];

You declared function as an array of pointers to pointers to
functions.

I don't think so. The OP's example functions return void * so I
copied that:

You're right, and I was wrong.
| typedef void* (*Fptr)(); /* ptr-to-function returning void* */

but I took the pointer-to-function out of the typedef.
I think you meant:

typedef void Function();

Function *function[FUNC_MAX];

All this does is change the return type from void * to void, does it
not?

Yes, you're right. My apologies.

Annoyed grunt.
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:
Ben Bacarisse said:
I have a slight preference for writing it this way:

typedef void *Function();

Function *function[FUNC_MAX];

You declared function as an array of pointers to pointers to
functions.

No, he didn't. Note the relative infrequency of parentheses in the
typedef.

Yup, I blew it. Sorry.

[...]
 
G

Guest

The functions are mainly used for getting and setting parameters in a
struct (size, position, text)

so do all these functions have the same prototype?

If not, how do call them...
typedef void* (*Fptr)();  /* ptr-to-function returning void*  */
Fptr function [FUNC_MAX];

Thx i like the typedef it makes it way more readable.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top