Ryan
Ryan

Reputation: 985

Converting from std::string to char * in C++

I'm using VS2012/C++, I need to convert a std::string to char * and I can't find any material online giving any guidance on how to go about doing it.

Any code examples and advice will be greatly appreciated.

Upvotes: 5

Views: 9805

Answers (1)

rubenvb
rubenvb

Reputation: 76519

Use

std::string bla("bla");
char* blaptr = &bla[0];

This is guaranteed to work since C++11, and effectively worked on all popular implementations before C++11.

If you need just a const char*, you can use either std::string::c_str() or std::string::data()

Upvotes: 14

Related Questions