1

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
  • 49,047
  • 44
  • 147
  • 241

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,717
  • 3
  • 29
  • 47
Michael Closson
  • 902
  • 8
  • 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
  • 95,107
  • 10
  • 109
  • 188