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

本文介绍了C++中string类型与char*、char[]之间的转换方法,并提供了实际的代码示例。重点讨论了使用c_str()和data()进行转换的区别及潜在问题。

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[]时不能直接赋值

__strcmp( const char *s1, const char *s2 ) { CYG_REPORT_FUNCNAMETYPE( "__strcmp", "returning %d" ); CYG_REPORT_FUNCARG2( "s1=%08x, s2=%08x", s1, s2 ); CYG_CHECK_DATA_PTR( s1, "s1 is not a valid pointer!" ); CYG_CHECK_DATA_PTR( s2, "s2 is not a valid pointer!" ); int retval = 0; CYG_UNUSED_PARAM( int, retval ); // in case tracing is off #if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__) while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; } retval = (*(unsigned char *) s1) - (*(unsigned char *) s2); CYG_REPORT_RETVAL( retval ); return retval; #else const CYG_WORD *aligned_s1; const CYG_WORD *aligned_s2; // If s1 or s2 are unaligned, then compare bytes. if (CYG_LIBC_STR_UNALIGNED2 (s1, s2)) { while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; } retval = (*(unsigned char *) s1) - (*(unsigned char *) s2); CYG_REPORT_RETVAL( retval ); return retval; } // If s1 and s2 are word-aligned, compare them a word at a time. aligned_s1 = (const CYG_WORD *)s1; aligned_s2 = (const CYG_WORD *)s2; while (*aligned_s1 == *aligned_s2) { // To get here, *aligned_s1 == *aligned_s2, thus if we find a // null in *aligned_s1, then the strings must be equal, so return // zero. if (CYG_LIBC_STR_DETECTNULL (*aligned_s1)) { CYG_REPORT_RETVAL( 0 ); return 0; } // if aligned_s1++; aligned_s2++; } // A difference was detected in last few bytes of s1, so search bytewise s1 = (const char *)aligned_s1; s2 = (const char *)aligned_s2; while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; } retval = (*(unsigned char *) s1) - (*(unsigned char *) s2); CYG_REPORT_RETVAL( retval ); return retval; #endif // not defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || // defined(__OPTIMIZE_SIZE__) } // __strcmp()分析下这个函数
08-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值