string和char*互转以及c_str()的陷阱

string类型和char*、char[]的转换

string转const char *:

1. 使用string的成员函数c_str();
2. 使用string的成员函数data();
3. 注意事项:c_str()data()的不同之处在于,data()会返回没有结束符的字符数组指针。并且需要强调一点,调用c_str()data()得到的指针指向的地址和原来的string是一个地址,所以只要string改变了,c_str()data()的返回值也会改变。
测试代码如下:

#include <iostream>
using namespace std;
#include <string>

int main()
{
    string s = "123123";
    const char *p1 = s.c_str();
    cout << "改变string之前的p1的值: ";
    cout << p1 << endl;
    s = "321321";
    cout << "改变string之后的p1的值: ";
    cout << p1 << endl;

    const char *p2 = s.data();
    cout << "改变string之前的p2的值: ";
    cout << p2 << endl;
    s = "123123";
    cout << "改变string之后的p2的值: ";
    cout << p2 << endl;

    return 0;
}

输出结果为:

改变string之前的p1的值: 123123
改变string之后的p1的值: 321321
改变string之前的p2的值: 321321
改变string之后的p2的值: 123123

这点特性很可能造成你的代码产生某些错误。需要警惕。
解决的方法也很简单,就是调用strcpy()函数将c_str()或者data()返回的字符串拷贝给另外一个字符串即可。

string转char *:

1. 使用string的成员函数copy();

char *转string:

1. 直接赋值即可

cha[]转string:

1. 使用string的成员函数copy()
2. 遍历赋值

代码如下:

#include <iostream>
using namespace std;
#include <string>
#include <cstdio>
int main()
{
    /*  string =====> const char*   */

    string s1 = "123123";
    const char *p1 = s1.c_str();
    cout << p1 << endl;

    const char *p2 = s1.data();
    cout << p2 << endl;

    /*  string =====> char *        */
    string s2 = "321321";
    char *p3 = NULL;
    p3 = (char *)malloc(s2.length() * sizeof(char));
    s2.copy(p3, s2.length(), 0);
    cout << p3 << endl;

    /*  char * =====> string        */
    char *p = "12341234";
    string s3 = p;
    cout << s3 << endl;

    /*  string =====> char []       */
    //方法一(使用copy函数:
    string s4 = "44444"
    char p4[6] = {0};
    s4.copy(p4, s4.length(), 0);
    p4[s4.length()] = '\0'; //填充结束符
    cout << p4 <<endl;          

    //方法二(遍历赋值:
    string s5 = "1232";
    char p5[5] = {0};
    int i;
    for(i = 0; i < s5.length(); i++)
    {
        p5[i] = s5[i];
    }
    p5[i] = '\0';
    cout << p5 << endl;
    return 0;

}

这里需要注意几点:

1. c_str()data()成员函数只能用于const char *类型
2. 字符数组需要自己添加结束符’\0’,不然会出现乱码。字符串(char*)则不需要。
3. string转char[]时不能直接赋值

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值