//【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; }
運行結果: