如何在 C++ 中将字符串转换为 char*?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4157644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:39:22  来源:igfitidea点击:

How do I convert a string to a char* in c++?

c++stringchar

提问by Ptichka

I have an error in my program: "could not convert from string to char*". How do I perform this conversion?

我的程序中有一个错误:“无法从字符串转换为字符*”。如何执行此转换?

回答by Martin Broadhurst

If you can settle for a const char*, you just need to call the c_str()method on it:

如果你能满足于 a const char*,你只需要调用c_str()它的方法:

const char *mycharp = mystring.c_str();

If you really need a modifiable char*, you will need to make a copy of the string's buffer. A vectoris an ideal way of handling this for you:

如果您确实需要一个可修改的char*,则需要制作字符串缓冲区的副本。Avector是为您处理此问题的理想方式:

std::vector<char> v(mystring.length() + 1);
std::strcpy(&v[0], mystring.c_str());
char* pc = &v[0];

回答by sbi

Invoke str.c_str()to get a const char*:

调用str.c_str()以获取const char*

const char *pch = str.c_str();

Note that the resulting const char*is only valid until stris changed or destroyed.

请注意,结果const char*str在更改或销毁之前有效。



However, if you really need a non-const, you probably shouldn't use std::string, as it wasn't designed to allow changing its underlying data behind its back. That said, you can get a pointer to its data by invoking &str[0]or &*str.begin().

但是,如果您确实需要非const,则可能不应该使用std::string,因为它的设计不允许在背后更改其基础数据。也就是说,您可以通过调用&str[0]或来获取指向其数据的指针&*str.begin()

The ugliness of this should be considered a feature. In C++98, std::stringisn't even required to store its data in a contiguous chunk of memory, so this might explode into your face. I think has changed, but I cannot even remember whether this was for C++03 or the upcoming next version of the standard, C++1x.

这种丑陋应该被认为是一个特征。在 C++98 中,std::string甚至不需要将其数据存储在连续的内存块中,因此这可能会引起您的注意。我认为已经改变,但我什至不记得这是针对 C++03 还是即将到来的下一版本标准 C++1x。

If you need to do this, consider using a std::vector<char>instead. You can access its data the same way: &v[0]or &*v.begin().

如果您需要这样做,请考虑使用 astd::vector<char>代替。您可以通过相同的方式访问其数据:&v[0]&*v.begin()

回答by Vatsan

//assume you have an std::string, str. 
char* cstr = new char[str.length() +1]; 
strcpy(cstr, str.c_str()); 

//eventually, remember to delete cstr
delete[] cstr; 

回答by John Dibling

Since you wanted to go from a stringto a char*(ie, not a const char*) you can do this BUT BEWARE: there be dragons here:

因为你想从 astring到 a char*(即,不是 a const char*)你可以这样做,但要注意:这里有龙:

string foo = "foo";
char* foo_c = &foo[0];

If you try to modify the contents of the string, you're well and truly on your own.

如果您尝试修改字符串的内容,那么您就完全靠自己了。

回答by aschepler

Use the c_str()method on a stringobject to get a const char*pointer. Warning: The returned pointer is no longer valid if the stringobject is modified or destroyed.

使用对象c_str()上的方法string来获取const char*指针。警告:如果string对象被修改或销毁,则返回的指针不再有效。

回答by Dialecticus

If const char*is good for you then use this: myString.c_str()

如果const char*对你有好处,那么使用这个:myString.c_str()

If you really need char*and know for sure that char*WILL NOT CHANGE then you can use this: const_cast<char*>(myString.c_str())

如果您确实需要char*并确定char*不会更改,那么您可以使用以下命令:const_cast<char*>(myString.c_str())

If char*may change then you need to copy the string into something else and use that instead. That something else may be std::vector, or new char[], it depends on your needs.

如果char*可能会更改,那么您需要将字符串复制到其他内容中并使用它。其他可能是std::vector,或者new char[],这取决于您的需要。

回答by Bertrand Marron

std::string::c_str()returns a c-string with the same contents as the stringobject.

std::string::c_str()返回与string对象内容相同的 c 字符串。

std::string str("Hello");
const char* cstr = str.c_str();