Hi,

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++] = "64.106.11.123";

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

Recommended Answers

All 3 Replies

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 = "127.0.0.1";
    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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.