Reputation: 49329
I would like to create a function that takes a variable number of void pointers,
val=va_arg(vl,void*);
but above doesn't work, is there portable way to achieve this using some other type instead of void*?
Upvotes: 1
Views: 4495
Reputation: 932
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
void
myfunc(void *ptr, ...)
{
va_list va;
void *p;
va_start(va, ptr);
for (p = ptr; p != NULL; p = va_arg(va, void *)) {
printf("%p\n", p);
}
va_end(va);
}
int
main() {
myfunc(main,
myfunc,
printf,
NULL);
return 0;
}
I'm using Fedora 14..
Upvotes: 6
Reputation: 96241
Since you have a C++ tag, I'm going to say "don't do it this way". Instead, either use insertion operators like streams do OR just pass a (const) std::vector<void*>&
as the only parameter to your function.
Then you don't have to worry about the issues with varargs.
Upvotes: 2