I have problem in using void pointers.
I want to pass parameter of array of unknown type. How could I implement it?
In the pointers article, there is an explanation of how to pass argument of unknown type, but just single element.
The problem which I couldn't find out is, how can I give the second/third/fourth/.... parameter value... Should I use pointer of void pointer?
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void pass(void **param)
{
//consider to pass integer
int *x[3];
x[0] = (int *)param[0];
x[1] = (int *)param[1];
x[2] = (int *)param[2];
}
void main()
{
int val[3] = {3,4,5};
void **param;
/*
I don't have any idea how to copy the values of val into param.
*/
pass(param);
}
void pass(void *param)
{
//consider to pass integer
int *x; // pointer to integers
x=(int*)param; // type-casting the pointer, not the elements
// x[0] already points to the first int
// x[1] already points to the second int
// x[2] already points to the third int
}
void main()
{
int val[3] = {3,4,5};
void *param; // void*, not pointer to void*
/*
I don't have any idea how to copy the values of val into param.
*/
param=(void*)val;
pass(param);
}
Notice that this does not "copy the values", but only the pointer. If you really need to copy the values, you can use either memcpy (for C guys) or the copy algorithm (the C++ way).