0

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*?

Hamza Yerlikaya
  • 48,407
  • 41
  • 141
  • 236

2 Answers2

6
#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..

richo
  • 8,209
  • 2
  • 28
  • 47
Michael Closson
  • 882
  • 9
  • 13
2

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.

Mark B
  • 93,381
  • 10
  • 105
  • 184