Can void-pointers be casted to function-pointers? 
Author Message
 Can void-pointers be casted to function-pointers?

Can void pointers be casted to functionpointers?

/Niclas



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?
I must me able to call a function who's address is stored in the void
pointer, from the void-pointer, for this I need casting.

/Niclas


Quote:

> > Can void pointers be casted to functionpointers?

> > /Niclas

> A void pointer can be assigned to any other pointer
> and it also can get the value of any other pointer.
> There should not be a need to cast it.

> Z



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?

Quote:

> Can void pointers be casted to functionpointers?

> /Niclas

A void pointer can be assigned to any other pointer
and it also can get the value of any other pointer.
There should not be a need to cast it.

        Z



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?

Quote:


> > Can void pointers be casted to functionpointers?

> A void pointer can be assigned to any other pointer
> and it also can get the value of any other pointer.
> There should not be a need to cast it.

Not true. A void * can be assigned to (and from) any _object_ pointer. A
function pointer is not an object pointer.

Richard



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?

Quote:

> I must me able to call a function who's address is stored in the void
> pointer, from the void-pointer, for this I need casting.

> /Niclas




> > > Can void pointers be casted to functionpointers?

> > > /Niclas

> > A void pointer can be assigned to any other pointer
> > and it also can get the value of any other pointer.
> > There should not be a need to cast it.

> > Z

So you probably want this:

int afunc(void);

int someotherfunc(int (*func)(void));

int main(void)
{
  void *ptr;

  ptr = &afunc;

  someotherfunc((int (*)(void))ptr);  

  return 0;

Quote:
}

        Z


Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?


Quote:
> Can void pointers be casted to functionpointers?

Well, yes and no.  The program segment:

   typedef void (*function_pointer)( void );

   int main() {
      void *a = "Foo";
      function_pointer b = (function_pointer)a;
      return 0;
   }

will compile without error, but (and this is a very big but) nothing can be
portably done with the contents of b.  Attempting to call it invokes
undefined behavior, and casting it back to a void pointer may not get back
the original contents.

What problem are you trying to solve with this?

--
poncho



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?

Quote:



>> Can void pointers be casted to functionpointers?
> Well, yes and no.  The program segment:
>    typedef void (*function_pointer)( void );
>    int main() {
>       void *a = "Foo";
>       function_pointer b = (function_pointer)a;
>       return 0;
>    }
> will compile without error, but (and this is a very big but) nothing can be
> portably done with the contents of b.  Attempting to call it invokes
> undefined behavior, and casting it back to a void pointer may not get back
> the original contents.

IINM, the null pointer can be cast portably from pointer to void to pointer
to function:
        int (*foo)() = (void *)0;

but that's not really important.  i *think* what the OP wants to do is
something like this:

struct {
        /* ... */
        void *generic_data;

Quote:
} foo;

int bar(int);

foo.generic_data = bar;

it would be very useful, but unfortunately it's not allowed in the C
standard.

--
             /"\                                m i k e    b u r r e l l

              X        AGAINST HTML MAIL
             / \



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?

Quote:




> > > > Can void pointers be casted to functionpointers?

> > > A void pointer can be assigned to any other pointer
> > > and it also can get the value of any other pointer.
> > > There should not be a need to cast it.

> > Not true. A void * can be assigned to (and from) any _object_ pointer. A
> > function pointer is not an object pointer.

> Hmm...I think one can assign a function pointer to a void pointer
> but not vice versa, am I right? At least one can assign the address
> of a function to a void pointer.

Oh, yes, you can _assign_ it all right, given a cast or two, but you can
say absolutely nothing about the result, because it is undefined.
Specifically, you can then not (at least not reliably) recast it back to
a function pointer and expect a useful function pointer back, or call a
funtion through that void pointer.

Richard



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?

Quote:



> > > Can void pointers be casted to functionpointers?

> > A void pointer can be assigned to any other pointer
> > and it also can get the value of any other pointer.
> > There should not be a need to cast it.

> Not true. A void * can be assigned to (and from) any _object_ pointer. A
> function pointer is not an object pointer.

> Richard

Hmm...I think one can assign a function pointer to a void pointer
but not vice versa, am I right? At least one can assign the address
of a function to a void pointer.
I believe my terminology is not propper here, this might be because
of english not being my first language. Appologies for this.

Look at my post replaying to Niclas.

        Z



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?

Quote:


>> Can void pointers be casted to functionpointers?
> A void pointer can be assigned to any other pointer
> and it also can get the value of any other pointer.
> There should not be a need to cast it.

no it can't.  to quote the standard:
       [#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.
and:
       [#1] object
       region of data storage in  the  execution  environment,  the
       contents of which can represent values
a function is not an object, so a pointer to a function can not necessarily
be represented by a pointer to void.

--
             /"\                                m i k e    b u r r e l l

              X        AGAINST HTML MAIL
             / \



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?
Niclas Larsn a crit dans le message

Quote:
>Can void pointers be casted to functionpointers?

No. A void* pointer is a generic data pointer. A function pointer is another
beast. If you want some generic function pointer, try:

void (*pf)();
or
int (*pf)();

--
-hs-
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"Learn to use your web search engine.  Someday it may save your life.
 Always keep your web search engine clean, dry, and serviceable."
--Dann Corbit CLC



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?

Quote:



>> > Not true. A void * can be assigned to (and from) any _object_ pointer. A
>> > function pointer is not an object pointer.

>> Hmm...I think one can assign a function pointer to a void pointer
>> but not vice versa, am I right? At least one can assign the address
>> of a function to a void pointer.
> Oh, yes, you can _assign_ it all right, given a cast or two, but you can
> say absolutely nothing about the result, because it is undefined.
> Specifically, you can then not (at least not reliably) recast it back to
> a function pointer and expect a useful function pointer back, or call a
> funtion through that void pointer.

his second (if you use a little imagination with the wording) is right
though: you can assign a pointer to a pointer to a function to a pointer to
void:
        int (*foo)();
        void *bar = &foo;

--
             /"\                                m i k e    b u r r e l l

              X        AGAINST HTML MAIL
             / \



Sat, 16 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?
On Tue, 30 May 2000 16:18:39 +0200, Zoran Cutura

Quote:

> > I must me able to call a function who's address is stored in the void
> > pointer, from the void-pointer, for this I need casting.

> > /Niclas




> > > > Can void pointers be casted to functionpointers?

> > > > /Niclas

> > > A void pointer can be assigned to any other pointer
> > > and it also can get the value of any other pointer.
> > > There should not be a need to cast it.

> > > Z

> So you probably want this:

> int afunc(void);

> int someotherfunc(int (*func)(void));

> int main(void)
> {
>   void *ptr;

>   ptr = &afunc;

The line above is a constraint violation.

Quote:
>   someotherfunc((int (*)(void))ptr);  

The cast makes this compile, but it produces undefined behavior if the
called function attempts to call through the pointer.

Quote:
>   return 0;
> }

>    Z

Jack Klein
--
Home: http://jackklein.home.att.net


Sun, 17 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?

Quote:

> On Tue, 30 May 2000 16:18:39 +0200, Zoran Cutura


> > > I must me able to call a function who's address is stored in the void
> > > pointer, from the void-pointer, for this I need casting.

> > > /Niclas




> > > > > Can void pointers be casted to functionpointers?

> > > > > /Niclas

> > > > A void pointer can be assigned to any other pointer
> > > > and it also can get the value of any other pointer.
> > > > There should not be a need to cast it.

> > > > Z

> > So you probably want this:

> > int afunc(void);

> > int someotherfunc(int (*func)(void));

> > int main(void)
> > {
> >   void *ptr;

> >   ptr = &afunc;

> The line above is a constraint violation.

> >   someotherfunc((int (*)(void))ptr);

> The cast makes this compile, but it produces undefined behavior if the
> called function attempts to call through the pointer.

Sorry but it runs on my gcc/linux box. So I definitly should have
turned on -ansi -pedantic to see that ansi complains about it.
You are absolutly right, my sollution is not portable, although it
did compile and run perfectly with my system.

gcc -Wall -ansi -pedantic -g -o funcp funcp.c
funcp.c: In function `main':
funcp.c:13: warning: ANSI forbids assignment between function pointer
and `void *'
funcp.c:15: warning: ANSI forbids assignment between function pointer
and `void *'

/* funcp.c */
#include<stdio.h>

int afunc(void);
int bfunc(void);

int someotherfunc(int (*func)(void));

int main(int argc, char **argv)
{
  void *ptr;

  if(argv[1][0] == 'a')
    ptr = &afunc;
  else
    ptr = &bfunc;

  someotherfunc((int (*)(void))ptr);  

  return 0;

Quote:
}

int afunc(void)
{
  printf("afunc\n");
  return 1;

Quote:
}

int bfunc(void)
{
  printf("bfunc\n");
  return 1;

Quote:
}

int someotherfunc(int (*func)(void))
{
  func();
  return 1;

- Show quoted text -

Quote:
}



Sun, 17 Nov 2002 03:00:00 GMT  
 Can void-pointers be casted to function-pointers?

Quote:




> >> > Not true. A void * can be assigned to (and from) any _object_ pointer. A
> >> > function pointer is not an object pointer.

> >> Hmm...I think one can assign a function pointer to a void pointer
> >> but not vice versa, am I right? At least one can assign the address
> >> of a function to a void pointer.

> > Oh, yes, you can _assign_ it all right, given a cast or two, but you can
> > say absolutely nothing about the result, because it is undefined.
> > Specifically, you can then not (at least not reliably) recast it back to
> > a function pointer and expect a useful function pointer back, or call a
> > funtion through that void pointer.

> his second (if you use a little imagination with the wording) is right
> though: you can assign a pointer to a pointer to a function to a pointer to
> void:
>         int (*foo)();
>         void *bar = &foo;

I have to correct myself, because this seems not to be in the
ANSI-standard.
Please see my replay to Jack Klein.

        Z



Sun, 17 Nov 2002 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. increment casted void pointer -- ANSI?

2. Casting function pointer to void pointer

3. pointers to functions and pointers to void.

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

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

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

7. Pointer Functions and Pointers to Pointer Functions

8. problem with pointer to pointer to void

9. pointer arithmetic with a pointer to void

10. Pointer-arithmatic and void pointers

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

12. function pointers and function pointers array

 

 
Powered by phpBB® Forum Software