I have a wired problem. I try to build a argv array which will be passed to ssh command.
The following line works perfectly:

argv[n++] = "";

But when I try to get the IP address out of a string which contains it, it will add me a line break after that argument.

argv[n++] = (char*)workerIp.c_str();

I even tried the data() method instead of c_str(), but it gives the same problem.

Could you tell me what is the difference between the first and the second/third line?
What I am doing wrong?

Thank you

Edited by wyssen: n/a

6 Years
Discussion Span
Last Post by wyssen

You must be doing something else that causes that '\n' at the end of the string. This works ok for me

int main()
    char *argv[20] = {0};
    std::string ip = "";
    argv[1] = (char*)ip.c_str();
    std::cout << argv[1] << '\n';
    std::cout << "Hello\n";


Note that the value returned by ip.c_str() persists only until the next time you change the value of ip . So if you modify ip in any way between the time you assign the value to argv[1] and the time you use the value, the effect is undefined.


It was actually the probelm Ancient Dragon pointed out. The string contained already a \n. Sorry, that was my fault and a dumb question.
Thanks for your help!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.