passing parameter of array of unknown type

Jan 28, 2008 at 2:39pm
Hi,

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);
}


Any idea?

Thanks before.

Best regards,
Heru
Jan 28, 2008 at 6:36pm
I think you have an issue with the level of indirection you are using for param.
I guess that what you are trying to do is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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).
Jan 29, 2008 at 1:45pm
You are right. I am a bit confused with this pointer, too much playing with Java :-(
Thanks for the help.

Regards,
Heru
Topic archived. No new replies allowed.