tags:

views:

52

answers:

1

I have a function that is supposed to take a variable number of arguments (using varargs) based on a format string:

void va(const char* name, const char* argformat, ...) {
    int numOfArgs = strlen(argformat);

    std::string buf = "asdf";

    va_list listPointer;
    va_start(listPointer, numOfArgs);
    char* blah;

    for(int i = 0; i < numOfArgs; i++) {
            switch (argformat[i]) {
                    case 's':
                            cout << va_arg(listPointer, char*) << endl;
                            break;
                    case 'i':
                            cout << va_arg(listPointer, int) << endl;
                            break;
                    case 'f':
                            cout << va_arg(listPointer, float) << endl;
                            break;
                    case 'b':
                            cout << va_arg(listPointer, bool) << endl;
                            break;
                    default:
                            break;
            }
    }

    va_end(listPointer);

    return;
}


int main() {
    va("fe", "sb", "asdf", true);

    return 0;
}

It crashes.

If I change
cout << va_arg(listPointer, char*) << endl;
to
cout << va_arg(listPointer, char) << end;

it prints "a".

What am I doing wrong?

+6  A: 
David
And that's what I get for not reading the docs. Thanks, this is the answer.
Frozen