string 是一個類,其中有一個 char * 類型的私有變量。 所以能夠以下構建一個string類型的變量。html
string str = "abcd";
其中,右值「abcd」一個字符串,存儲在常量區的連續內存中,以 '\0' 做爲結束標誌位,返回一個指向該段內存起始位置的 char * 指針。經過重載賦值運算符 = ,能夠使用 char * 類型變量給str賦值。web
https://blog.csdn.net/u012611878/article/details/78291036spa
char a[] = "ABC"; // return char *, in stack char b[] = {'A', 'B', 'C', ' '}; // return char *, in stack const char* c[] = {"A","B"}; // return (const char *)*, for example int v[10] = {1},return int * const char *d = "ABC"; cout << "In constant area :" << (void*)"ABC" << endl; cout << "In stack area :" << (void*)a << endl; cout << a << endl; cout << a[0] << endl; cout << sizeof(a)/sizeof(char) << endl; cout << "********************" << endl; cout << "In stack area :" << (void*)b << endl; cout << b << endl; cout << b[0] << endl; cout << sizeof(b)/sizeof(char) << endl; cout << "********************" << endl; cout << c << endl;//void* cout << c[0] << endl; cout << sizeof(c)/sizeof(c[0]) << endl; cout << "********************" << endl; cout << "In constant area :" << (void*)d << endl; cout << d << endl; cout << *d << endl; cout << d[0] << endl; cout << sizeof(d)/sizeof(d[0]) << endl;
輸出char指針地址.net
因爲C++重載輸出運算符 << ,把 char * 類型的變量當作字符串變量。因而當遇到 char * 類型的變量時,輸出指針指向的內容。若是想輸出 char * 變量的地址,必須使用強制類型轉換,把字符指針轉換爲無類型指針。指針
const char * m_data = "abcd"; cout << static_cast<const void *>(m_data) << endl;