【发布时间】:2018-09-25 03:50:34
【问题描述】:
我认为 const char* 表示指向不可变字符串的可变指针。
但是,当我这样做时,
#include <iostream>
using namespace std;
const char *name1 = "Alex";
int main()
{
name1 = "John";
cout << name1 << endl;
}
它只是打印 John 并且没有显示任何问题。我想知道为什么程序将 name1 视为字符串并使其可变?
【问题讨论】:
-
它是“字符数组,零终止”意义上的“字符串”,而不是
std::string意义上的“字符串”。指针是可变的。要创建一个 const 指针,const char * const name1 = "Alex";.