字符串类型在 C++ 中的 strcpy() 中
string type in strcpy() in C++
#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());
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- .cpp和.h文件中的模板专用化声明
- 反向给定链表中的K节点
- 正在查找文档以获得PS4平台的C++中的设备信息
- enum是C++中的宏变量还是整数变量
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 将字符串存储在c++中的稳定内存中
- 文本文件中的单词链表
- 递归函数计算序列中的平方和(并输出过程)
- 如何从C++中的依赖类型中获得它所依赖的类型
- C++中的"inline"关键字
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 用C++中的一个变量定义一个常量
- vector.resize()中的分配错误
- 使用指针从C++中的数组中获取最大值
- 使用 strcpy 时构造函数中的分段错误
- 字符串类型在 C++ 中的 strcpy() 中
- c++控制台程序中strcpy函数的异常.strcpy()中的
- 不了解示例中的行为 - strcpy() 和返回本地数组地址的函数