Re: Baffled by this ... (casting of function pointers)



On Thu, 25 Aug 2005 20:43:33 -0700, Barry Schwarz wrote:

> On Fri, 26 Aug 2005 00:42:00 +0000 (UTC), Alfonso Morra
> <sweet-science@xxxxxxxxxxxx> wrote:
>
>>How can this work ?
>>
>>I have the following declarations in a header:
>>
>>Header
>>=========
>>typedef void (*VFPTR)(void) ;
>>void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*);
>>int bar(int *, char *, VFPTR, void *);
>>
>>Source
>>=========
>>In the C source code, I come accross this line:
>>
>>bar(&id, name, FOO_Callback, (void *)handle);
>
> The cast on handle is superfluous if it is a pointer of any type.
> Since the prototype for bar is known, any pointer type will be
> implicitly converted void* as part of evaluating the arguments.

Only pointers to unqualified object or imcomplete types can be converted
implicitly to void *. Function pointers can't nor can pointers with types
like const int * or indeed const void *. However this is a good thing and
attempts at conversions like these should be warned about so the code is
much better without the cast.

Lawrence
.



Relevant Pages

  • Re: Header Interfaces (Part 2)
    ... This is why people provide compiled libraries and not source code. ... the header while we hide the source code. ... void f1; ... Put the #define in the header file. ...
    (microsoft.public.vc.language)
  • Re: Header Interfaces (Part 2)
    ... But in my case I will have to allow him to modify 3 of them. ... source code, because that is where the value is, and what is most easily stolen. ... the header while we hide the source code. ... void f1; ...
    (microsoft.public.vc.language)
  • Re: DLL Imports
    ... relative pointers there are sometimes these numbers with the 8th bit set. ... Import Header Address: 01042AB0 ... Original Thunk: 00043238 ... Function Index: 1 ...
    (microsoft.public.win32.programmer.kernel)
  • Re: sizeof(ptr) = ?
    ... A void * has the same representation as a char *. ... Char pointers which require additional data ... iff the default offset is 0. ...
    (comp.lang.c)
  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)