Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how initialize char* const argv [] in c++

Tags:

c++

c

An API function takes an argument of type 'char *const argv[]' I am initializing this type of arguments in my c++ application like:

char* const  argv[] = {"--timeout=0", NULL}; 

and passing the arguments to API function like:

Spawner spawner;
spawner.execute (argv, true);

using g++ compiler, I am getting following Error:

error: deprecated conversion from string constant to 'char*' [-Werror=write-strings]

how can I get rid of above error?

Below is the declaration of execute function in API:

void Spawner::execute (char *const argv[], bool bShowChildWindow)
like image 268
hmb Avatar asked Apr 29 '26 08:04

hmb


1 Answers

Yeah, C++ is annoyingly (but correctly) strict about const chars. Try this

char timeoutString[] = "--timeout=0";    // make a non-const char array
char *argv[] = { timeoutString, NULL };
Spawner spawner;
spawner.execute( argv, true );

Technically, the problem is with the declaration of the execute method, which should be

void Spawner::execute (const char *const argv[], bool bShowChildWindow)

assuming execute doesn't modify the strings or the array.

like image 141
user3386109 Avatar answered Apr 30 '26 21:04

user3386109