【问题标题】:string type in strcpy() Cstrcpy() C 中的字符串类型
【发布时间】:2013-10-26 19:18:14
【问题描述】:
#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);
    return 0;
}

我知道 strcpy 被定义为

char * strcpy ( char * destination, const char * source );

但是如果string类型变量与char*char[]相同,为什么string类型的内容不能复制到char[]

请赐教。

【问题讨论】:

    标签: c++ string char strcpy


    【解决方案1】:

    std::string 与字符数组相同。它是一个 C++ 对象。如果你想以 C 字符串的形式访问它,调用它的c_str() 方法:

    strcpy(a, b.c_str());
    

    【讨论】:

      猜你喜欢
      • 2014-10-01
      • 2018-05-08
      • 2016-03-16
      • 2021-03-28
      • 2020-10-24
      • 2011-07-04
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      相关资源
      最近更新 更多