Does anyone have an idea of how to write functions that accept a
void pointer?
eg. fread and fwrite both accept "void* buffer"
how does the compiler know how to cast it, or is this only done
in assembly?
Does anyone have an idea of how to write functions that accept a
void pointer?
eg. fread and fwrite both accept "void* buffer"
how does the compiler know how to cast it, or is this only done
in assembly?
It's of type 'void'. It can be dereferenced as whatever you want. The compiler simply looks at what you're making an assignment to, and it dereferences it as such. In C++ you have to manually type cast it.
Quzah.
Hope is the first step on the road to disappointment.
In this example, the function myfunc takes the void pointer and assigns it to an int pointer. You can then use ip in the normal manner.Code:#include <stdio.h> void myfunc(void *vp) { int *ip = vp; printf ("The int value is %d\n", *ip); } int main(void) { int i = 10; myfunc(&i); return 0; }
This particular example is a bit lame, but it gives you the idea.
One more thing, you must assign the void pointer to a known type in order to do arithmatic with it. See http://www.eskimo.com/~scs/C-faq/q11.24.html
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]