Hamza Yerlikaya
Hamza Yerlikaya

Reputation: 49329

va_arg with a list of void*

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

Answers (2)

Michael Closson
Michael Closson

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

Mark B
Mark B

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

Related Questions