#include <iostream>
#include <string>
using namespace std;
int main() {
char a[10];
string b = "Hello";
char c[] = "Hello";
char *d = "Hello";
strcpy(a, b); // compiler complains.
strcpy(a, c);
strcpy(a, d);
}
我知道 strcpy 定义为
char * strcpy ( char * destination, const char * source );
但是如果string
类型变量与char*
或char[]
相同,为什么string
的内容> 类型无法复制到char[]
?
最佳答案
std::string
与字符数组不同。它是一个 C++ 对象。如果您想以 C 字符串的形式访问它,请调用它的 c_str()
方法:
strcpy(a, b.c_str());
关于c++ - C++ 中 strcpy() 中的字符串类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19441311/