Author |
Message |
Niclas Larsé #1 / 17
|
 Can void-pointers be casted to function-pointers?
Can void pointers be casted to functionpointers? /Niclas
|
Sat, 16 Nov 2002 03:00:00 GMT |
|
 |
Niclas Larsé #2 / 17
|
 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 |
|
 |
Zoran Cutur #3 / 17
|
 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 |
|
 |
Richard B #4 / 17
|
 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 |
|
 |
Zoran Cutur #5 / 17
|
 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 |
|
 |
Scott Fluhre #6 / 17
|
 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 |
|
 |
mike burrel #7 / 17
|
 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 |
|
 |
Richard B #8 / 17
|
 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 |
|
 |
Zoran Cutur #9 / 17
|
 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 |
|
 |
mike burrel #10 / 17
|
 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 |
|
 |
-hs- #11 / 17
|
 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 |
|
 |
mike burrel #12 / 17
|
 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 |
|
 |
Jack Klei #13 / 17
|
 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: Jack Klein -- Home: http://jackklein.home.att.net
|
Sun, 17 Nov 2002 03:00:00 GMT |
|
 |
Zoran Cutur #14 / 17
|
 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; Quote: }
|
Sun, 17 Nov 2002 03:00:00 GMT |
|
 |
Zoran Cutur #15 / 17
|
 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 |
|
|