指针是const
- 表示一旦得到了某个变量的地址,不能在指向别的其他变量。
- int * const q = &i; //q是const
- 指针不可修改
- *q = 26;//OK
- q++; //ERROR
所指是const
- 表示不能通过这个指针去修改那个变量(并不能使得那个变量成为const)
- const int *p = &i;
- *p=26;//error!(*p)是const
- 通过指针不可修改
- i=26;//ok
- p=&j;//ok
判断哪个被const了的标志是const在*的前面还是后面
- const在*前面:通过指针不可修改
- *在const前面:指针不可修改
判断
int i;
const int* p1=&i;
int const* p2=&i;
int *const p3=&i;
保护数组值
- 因为把数组传入函数时传递的是地址,所以那个函数内部可以修改数组的值
- 为了保护数组不被函数破坏,可以设置参数为const
- int sum(const int a[],int length);
笔记来自浙江大学翁凯老师C语言慕课视频