casting void pointer to be a pointer to a function 
Author Message
 casting void pointer to be a pointer to a function


Quote:
> int (* abort_func) ();
...
> abort_func = (int *() ) value;
...
> I try and compile it, and I get this cute little message: "cast
> specifies function type"

The message means that you have tried to cast `value' to be a function,
which it is not (it is a pointer, and in general a cast cannot convert
any C object into a function).  Instead, you must cast `value' into a
*pointer* to a function (of unspecified parameters, returning int), like
so:

  abort_func = (int (*)()) value;

P.S. If you intend that `abort_func' should take no parameters, then
give it the type `int(*)(void)' rather than `int(*)()'.  Perhaps it's
time to re-read Kernighan and Richie, 2nd edition, section 5.11; also pp
220-221.

--
Gareth Rees



Sun, 10 May 1998 03:00:00 GMT  
 casting void pointer to be a pointer to a function

Quote:
Bhattacharya) writes:

|> >But there is a more serious problem ...
|> >
|> >
|> >Yes, this is the problem. void* is big enough to point to any `object'
|> >whatsoever: but it is not necessarily big enough to point to a function.
|>
|> This is new to me, so I picked my C-Bible (K&H) and read "Pointer to
functions".
|> There I find: "...Every pointer can be transformed to void * and vice
versa without
|> loosing information ..." (This is a re-translated sentence from the german
version).

The statement is wrong. Christianity may be defined by the bible (in
paractice, even that is not true), but C is defined by the ANSI C standard
and the interepretation rulings.:-)

|> I think this is logical. Otherwise there would be no possibilty to declare
library
|> functions like qsort().

How will you get an array of functions to pass to qsort in the first place?
Think before you write.

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Mon, 11 May 1998 03:00:00 GMT  
 casting void pointer to be a pointer to a function

Quote:


>>But there is a more serious problem ...

>>Yes, this is the problem. void* is big enough to point to any `object'
>>whatsoever: but it is not necessarily big enough to point to a function.

>This is new to me, so I picked my C-Bible (K&H) and read "Pointer to functions".
>There I find: "...Every pointer can be transformed to void * and vice versa without
>loosing information ..." (This is a re-translated sentence from the german version).
>I think this is logical. Otherwise there would be no possibilty to declare library
>functions like qsort().

Get a new bible.  ISO 6.2.2.3 and 6.3.4 define the permissible
conversions among pointers.  Conversion between pointer to object and
pointer to function isn't there.

qsort(), at least as described by the standard, does not need a
conversion between void* and pointer to function.  The comparison
parameter is a pointer to function and the corresponding argument must
be of the same type pointer to function.

Michael M Rubenstein



Mon, 11 May 1998 03:00:00 GMT  
 casting void pointer to be a pointer to a function

Quote:


>>But there is a more serious problem ...

>>Yes, this is the problem. void* is big enough to point to any `object'
>>whatsoever: but it is not necessarily big enough to point to a function.
>This is new to me, so I picked my C-Bible (K&H) and read "Pointer to functions".
>There I find: "...Every pointer can be transformed to void * and vice versa without
>loosing information ..." (This is a re-translated sentence from the german version).
>I think this is logical. Otherwise there would be no possibilty to declare library
>functions like qsort().

This is a "non sequitur". Proof:

qsort() takes a pointer to a function that takes two pointers to const void
as it's last arguments. The last argument of qsort is _not_ a void pointer,
so it _is_ possible to define qsort() even if a void pointer cannot hold a
pointer to a function.

There are relevant implementations where the size of a data pointer differs
from the size of a function pointer. Hence a promise that void pointers
have to be able to hold pointers to functions would, in such an implementation,
require an even more generic pointer than a "general" data pointer.

I fail to see a real need for "generic" function pointers, because if
you introduce them to a language, you have to pass around the equivalent
of the functions "signature" anyway, so that you can call the function
with the right arguments and handle the return value correctly.

Exceptions are typeless languages with an UPN notation.

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-6254




Mon, 11 May 1998 03:00:00 GMT  
 casting void pointer to be a pointer to a function

Quote:
>I try and compile it, and I get this cute little message:
>"cast specifies function type" for print_func and abort_func fields.

>But I want that! I want value to take an integer, a string, and functions!
>Why does the compiler barf?

Because, unlike you, it knows C :-)

Quote:
>I thought void * meant you were just passing a pointer to some object.

Right.  In C, functions aren't objects, however.

Quote:
>Would the
>size of the pointer be different if it referenced a function as opposed to something
>like an integer etc.?

Yes, on certain platforms.  Like MSDOS, in the medium and compact memory
models.

Quote:
>Any compiler dependencies etc I should know about?

Nope.  You should know the language itself.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Tue, 12 May 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Casting function pointer to void pointer

2. casting void pointer to be a pointer to a function

3. Casting function pointer to void*

4. Q: casting to a pointer to a function returning void

5. casting of struct element void pointer casting

6. pointers to functions and pointers to void.

7. Can void-pointers be casted to function-pointers?

8. How to use (pointer to function), and function and pointer to (pointer to function)

9. Casting a method pointer to a function pointer.

10. Pointer Functions and Pointers to Pointer Functions

11. Casting pinning pointer to void *

12. Casting from a void pointer

 

 
Powered by phpBB® Forum Software