C++中的字符數組與字符指針


//【C++基礎】字符數組和字符指針.cpp
//劍指offer上的這段話:
//為了節省內存,c/c++把常量字符串放到單獨的一個內存空間。但是當幾個指針賦值給相同的常量字符串時,它們實際上會指向相同的內存地址
//

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
    char str1[] = "hello world";
    char str2[] = "hello world";

    const char* str3 = "hello world";  //定義:char* str3 = "hello world";  報錯
    const char* str4 = "hello world";

    if (str1 == str2)
        cout << "str1 and str2 are same" << endl;
    else
        cout << "str1 and str2 are not same" << endl;

    if (str3 == str4)
        cout << "str3 and str4 are same" << endl;
    else
        cout << "str3 and str4 are not same" << endl;
    return 0;
}

 

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM