4

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"};
2
  • 1
    You want const char* cwd[] Commented Jan 30, 2019 at 1:20
  • 2
    And then you want const char **argc) in processArgs(). Commented Jan 30, 2019 at 1:36

2 Answers 2

6

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 };
Sign up to request clarification or add additional context in comments.

1 Comment

That's really nice answer.
4

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") };

12 Comments

Please consider user207421's comment. Otherwise, option 1 will give compilation error.
@KunalPuri Edited my post, see my new disclaimer near option 2
I am more concerned about option 1. Because that is much better than option 2. Also, OP is supposed to change the argument type for option 1 not for option 2. I think that disclaimer should be part of option 1.
@KunalPuri Not sure what you're saying
void processArgs(int argc, char** argv); This is the declaration of function used by OP. So, What I want to say is that for option 1, you have to change it to void processArgs(int argc, const char** argv);. For option 2, I think void processArgs(int argc, char** argv); will work just fine as far as compilation is concerned.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.