C++ char*[] to char** conversion

Viewed 4580

I have this simple code that compiles without errors/warnings:

void f(int&, char**&){}

int main(int argc, char* argv[])
{
    f(argc, argv);
    return 0;
}

And next similar code that doesn't compile:

void f(int&, char**&){}

int main()
{
    int argc = 2;
    char* argv[] = { "", "", nullptr };
    f(argc, argv); 
    //@VS2013 error: cannot convert argument 2 from 'char *[3]' to 'char **&'
    //@GCC error: invalid initialization of non-const reference of type 'char**&' from an rvalue of type 'char**'
    return 0;
}

Why char*[] can be converted to char**& in the first sample and can't be converted in the second sample? Does it matter if the size is known at compile time?

EDIT: I think there are 2 conversions needed in the second case, and only one implicit conversion can be done by compiler.

This code compiles fine:

void f(int&, char**&){}

int main()
{
    int argc = 2;
    char* temp[] = { "", "", nullptr };
    char** argv = temp;
    f(argc, argv);
    return 0;
}
3 Answers
Related