tags:

views:

63

answers:

2

Hi,

I am getting the following error with gcc.

invalid conversion from ‘char**’ to ‘const char**’

With this code.

void foo( const int &argc, const char **argv );

int main( int argc, char *argv[] )
{
   foo( argc, argv );                                                            
}

Why is this?

+7  A: 

When used in function parameter list, char *argv[] declaration is equivalent to char **argv declaration. For this reason, when you are passing argv to foo you are actually attempting to convert argv from char ** type to const char ** type. This is illegal. Read the FAQ http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17 for why it is illegal.

AndreyT
+1 for the useful link
Dacav
A: 

I suggest change foo too:

void foo( const int &argc, const char* const* const& argv);
ocsi
I suggest a change to `void foo( int argc, const char* const* argv);` The references-to-const are nothing but noise in this case.
sellibitze