const指针顾名思义就是常量指针的意思。下面将经过一些具体例子说明const指针的几种用法。ios
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int yes = 100; 8 const int *p = &yes; 9 10 *p = 10; //Error assign! 11 12 return 0; 13 }
用g++编译的结果是:c++
error: assignment of read-only location ‘* p’.
7 const int yes = 100; 8 int *p = &yes;
用g++编译的错误为:invalid conversion from ‘const int*’ to ‘int*’ .