Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to covert string array to char**

Tags:

c++

c++11

I want to send the values manually here

void processArgs(int argc, char** argv);

if I sending like this

char* cwd[] = {"./comDaemon", "--loggg=pluginFramework:debug"};

parser->processArgs(2, cwd);

compiler showing warning as

warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]

 char* cwd[] = {"./comDaemon", "--loggg=pluginFramework:debug"};
like image 524
jack sparow Avatar asked Dec 24 '22 01:12

jack sparow


2 Answers

Others have noted that the problem is you're trying to pass string literals (which are const) to a function that takes a non-const char ** argument. If what you want is to create non-const strings that you can pass to your non-const arg function, you need explicit char arrays (which you can initialize with string literals):

char arg0[] = "./comDaemon";
char arg1[] = "--loggg=pluginFramework:debug";
char *cwd[] = { arg0, arg1 };

you could even do this all on one line:

char arg0[] = "./comDaemon", arg1[] = "--loggg=pluginFramework:debug", *cwd[] = { arg0, arg1 };
like image 77
Chris Dodd Avatar answered Jan 06 '23 11:01

Chris Dodd


If the function you're passing cwd to expects char ** argument, instead of const char **, here is one way:

    char *cwd[] = { const_cast<char *>("value1"), const_cast<char *>("value2") };
like image 24
Govind Parmar Avatar answered Jan 06 '23 11:01

Govind Parmar