Problem passing a reference as a named parameter to a variadic function

Posted by Michael Mrozek on Stack Overflow See other posts from Stack Overflow or by Michael Mrozek
Published on 2010-05-17T21:48:44Z Indexed on 2010/05/17 21:50 UTC
Read the original article Hit count: 263

I'm having problems in Visual Studio 2003 with the following:

void foo(const char*& str, ...) {
    va_list args;
    va_start(args, str);

    const char* foo;
    while((foo = va_arg(args, const char*)) != NULL) {
        printf("%s\n", foo);
    }
}

When I call it:

const char* one = "one";
foo(one, "two", "three", NULL);

I get:

Access violation reading location 0xcccccccc

on the printf() line -- va_arg() returned 0xcccccccc. I finally discovered it's the first parameter being a reference that breaks it -- if I make it a normal char* everything is fine. It doesn't seem to matter what the type is; being a reference causes it to fail at runtime. Is this a known problem with VS2003, or is there some way in which that's legal behavior? It doesn't happen in GCC; I haven't tested with newer Visual Studios to see if the behavior goes away

© Stack Overflow or respective owner

Related posts about c

    Related posts about visual-studio