Darren
Darren

Reputation: 49

Converting a char * to string *

I am trying to convert a char pointer to a string pointer but I am not sure if I am doing it correctly. I just wanted to post what I was trying and see if it was correct.

For context, I have a char * called ent->d_name and I need that to become a string *. This is what I have been doing:

std::string arg = std::string(ent->d_name);
std::string * arg_p = &arg;
Command::_currentCommand->insertArgument(arg_p);

The insert command function takes a string pointer.

Upvotes: 2

Views: 133

Answers (1)

Sid S
Sid S

Reputation: 6125

You could use

std::string *arg_p = new std::string(ent->d_name);

It will create a memory leak unless you delete each string after use, but apart from that it will work.

Upvotes: 1

Related Questions