pointers to functions and pointers to void. 
Author Message
 pointers to functions and pointers to void.

I had to look up in the standard today and there i found:

6.3.2.3 Pointers
1 A pointer to void may be converted to or from a pointer to any
   incomplete or object type. A pointer to any incomplete or object type
   ^^^^^^^^^^^^^^^^^^^^^^^^^
   may be converted to a pointer to void and back again; the result shall
   compare equal to the original pointer.

So, if I read this correctly and there is no other clause which
contradicts this I can't make a pointer to void point to a
function and later convert the pointer back to a function pointer.
Is this correct?

So, AFAICS the only way to do this is to wrap the function pointer
up in a struct. Anyone has any other ideas? The reason I ask is that
I am using a generic datastructures and now I need to fill it with
pointers...

--
Thomas.



Sun, 10 Apr 2005 01:20:10 GMT  
 pointers to functions and pointers to void.

Quote:

> So, if I read this correctly and there is no other clause which
> contradicts this I can't make a pointer to void point to a
> function and later convert the pointer back to a function pointer.
> Is this correct?

Right.  Function and object pointers are not inter-convertible,
not even through a pointer to void.

Quote:
> So, AFAICS the only way to do this is to wrap the function pointer
> up in a struct. Anyone has any other ideas? The reason I ask is that
> I am using a generic datastructures and now I need to fill it with
> pointers...

Only way to do what?  If you want to have a single pointer that
can point to a function or an object, you're out of luck.  Use a
union of void * and void (*)() instead.
--
"The expression isn't unclear *at all* and only an expert could actually
 have doubts about it"
--Dan Pop


Sun, 10 Apr 2005 02:46:37 GMT  
 pointers to functions and pointers to void.

Quote:
> 6.3.2.3 Pointers
> 1 A pointer to void may be converted to or from a pointer to any
>    incomplete or object type. A pointer to any incomplete or object type
>    ^^^^^^^^^^^^^^^^^^^^^^^^^
>    may be converted to a pointer to void and back again; the result shall
>    compare equal to the original pointer.

> So, if I read this correctly and there is no other clause which
> contradicts this I can't make a pointer to void point to a
> function and later convert the pointer back to a function pointer.
> Is this correct?

Correct. A void* can't hold portably the address of a function.

Quote:
> So, AFAICS the only way to do this is to wrap the function pointer
> up in a struct. Anyone has any other ideas? The reason I ask is that
> I am using a generic datastructures and now I need to fill it with
> pointers...

You can use an union if you supply a way to do the selection.

typedef enum
{
   GS_OBJECT,
   GS_FUNCTION,
   GS_dummy

Quote:
}

generic_selector_e;

typedef int generic_f();

typedef union
{
   void *p_object;
   generic_f *p_function;

Quote:
}

generic_u;

typedef struct
{
   generic_selector_e select;
   generic_u u;

Quote:
}

generic_s;

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/htm_cl/index.html
"Mal nommer les choses c'est ajouter du malheur au monde."
-- Albert Camus.



Sun, 10 Apr 2005 03:07:18 GMT  
 pointers to functions and pointers to void.

Quote:

> I had to look up in the standard today and there i found:

> 6.3.2.3 Pointers
> 1 A pointer to void may be converted to or from a pointer to any
>    incomplete or object type. A pointer to any incomplete or object type
>    ^^^^^^^^^^^^^^^^^^^^^^^^^
>    may be converted to a pointer to void and back again; the result shall
>    compare equal to the original pointer.

> So, if I read this correctly and there is no other clause which
> contradicts this I can't make a pointer to void point to a
> function and later convert the pointer back to a function pointer.
> Is this correct?

    Yes.  Pointers to data and pointers to code are entirely
different beasts in C.  Any confusion between them is due entirely
to syntactic similarity, not to a semantic relation.

    If you like, think of them as `long' and `double'.  Both are
numeric, both can be used with `+' and `==' -- but they are
clearly not the same sort of thing, and you can lose information
by converting back and forth between them (in either direction,
usually).

Quote:
> So, AFAICS the only way to do this is to wrap the function pointer
> up in a struct. Anyone has any other ideas? The reason I ask is that
> I am using a generic datastructures and now I need to fill it with
> pointers...

    "The only way to do this ..."  I'm not sure precisely what "this"
is.  Here's what you cannot do: make a `void*' point to a function.
(Nor can you make a function pointer point to an `int'.)  However,
a function pointer itself is a data object, and you *can* make a
`void*' point to such a thing:

        double (*f)(double) = sqrt;
        void *vp = &f;

That is, "pointer to pointer to function" is perfectly all right.
Not sure whether this helps or hinders ...

--



Sun, 10 Apr 2005 03:12:34 GMT  
 pointers to functions and pointers to void.

Quote:


>> So, if I read this correctly and there is no other clause which
>> contradicts this I can't make a pointer to void point to a
>> function and later convert the pointer back to a function pointer.
>> Is this correct?

> Right.  Function and object pointers are not inter-convertible,
> not even through a pointer to void.

Hmm, that brings to mind a related question.  Is a pointer-to-function
itself an 'object', so that a pointer-to-pointer-to-function can be
converted to a pointer to void?

        - Kevin.



Sun, 10 Apr 2005 22:19:18 GMT  
 pointers to functions and pointers to void.
* Kevin Easton
| Is a pointer-to-function itself an 'object', so that a
| pointer-to-pointer-to-function can be converted to a pointer to
| void?

Yes.



Sun, 10 Apr 2005 22:24:23 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Casting function pointer to void pointer

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

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

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

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

6. Pointer Functions and Pointers to Pointer Functions

7. problem with pointer to pointer to void

8. pointer arithmetic with a pointer to void

9. Pointer-arithmatic and void pointers

10. Question about signal()/pointers to functions that return pointers to functions

11. function pointers and function pointers array

12. C++ function pointers versus C function pointer problem

 

 
Powered by phpBB® Forum Software