Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Must a function pointer have a prototype in C

Reply
Thread Tools

Must a function pointer have a prototype in C

 
 
mireillen@gmail.com
Guest
Posts: n/a
 
      02-27-2009
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")
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      02-27-2009
wrote:
> 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.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
 
 
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      02-27-2009
On 27 Feb, 10:36, mireil...@gmail.com wrote:
> 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?


--
Nick Keighley

A coding theorist is someone who doesn't think Alice is crazy.
 
Reply With Quote
 
mireillen@gmail.com
Guest
Posts: n/a
 
      02-27-2009

> 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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-27-2009
writes:

>> 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.

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-27-2009
Ben Bacarisse <> writes:
[...]
> 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];

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-27-2009
Keith Thompson <kst-> writes:

> Ben Bacarisse <> writes:
> [...]
>> 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?

--
Ben.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-28-2009
Ben Bacarisse <> writes:
> Keith Thompson <kst-> writes:
>> Ben Bacarisse <> writes:
>> [...]
>>> 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.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-28-2009
Richard Heathfield <> writes:
> Keith Thompson said:
>> Ben Bacarisse <> writes:
>> [...]
>>> 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.

[...]

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      02-28-2009
On 27 Feb, 12:20, mireil...@gmail.com wrote:

> > 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)


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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prototype WTP 0.2 released,this release for Prototype 1.6.0 javascript fish Javascript 0 10-11-2008 07:35 AM
Class prototype vs C function prototype June Lee C++ 2 04-13-2008 08:17 PM
Prototype Object.extend(new Base() | Hash | Hash.prototype) usage: jacobstr@gmail.com Javascript 3 03-27-2007 07:56 AM
relation between prototype and Prototype.js shypen42@yahoo.fr Javascript 9 05-26-2006 01:13 AM
Function pointer prototype interpretation Alfonso Morra C Programming 5 08-30-2005 11:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57