码迷,mamicode.com
首页 > 其他好文 > 详细

string,const char*, char*转换之后还是指向同一片内存地址么?

时间:2019-08-18 20:01:24      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:out   eve   ons   void   his   const   reverse   main   this   

class Same {
public:
    void reverse(const char* str)
    {
        char* A = const_cast<char*>(str);
        A[0] = 1;

        printf("%u\n",A);
        printf("%u\n",str);
        return;
    }
};


int main()
{
    Same re;
    string str = "This is nowcoder";
    printf("%u\n", &str);
    re.reverse(str.data());
    cout<< str << "  " << endl;
    return 0;
}

为什么str的值被修改了呢?

技术图片

string,const char*, char*转换之后还是指向同一片内存地址么?

标签:out   eve   ons   void   his   const   reverse   main   this   

原文地址:https://www.cnblogs.com/yiyi20120822/p/11373291.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!