面试题答案
一键面试#include <iostream>
int main() {
// const char *p 表示指针指向的内容是常量,指针本身可变
const char *p1 = "Hello";
// 下面这行代码会报错,因为p1指向的内容是常量,不能修改
// *p1 = 'h';
p1 = "World"; // 正确,指针p1本身可以改变指向
// char * const p 表示指针本身是常量,指针指向的内容可变
char str[] = "Hello";
char * const p2 = str;
*p2 = 'h'; // 正确,指针p2指向的内容可以修改
// 下面这行代码会报错,因为p2是常量指针,不能改变指向
// p2 = "World";
std::cout << "p1: " << p1 << std::endl;
std::cout << "p2: " << p2 << std::endl;
return 0;
}