之前面试遇到了一道题,这也就是相当于记录一下而已。
你觉得答案如何呢?
1.指针常量
意思:它是一个常量,但是通过指针修饰了一下。
表达:int * const p
#include <stdio.h>
int main()
{
int a,b;
int *const p=&a;
*p=1;
p=&b;//这里是错误的
printf("%d\n",*p);//这个毫无疑问可以打印出来1
printf("%p\n",p);//这个可以打印出来什么?
return 0;
}
结论:说明指针指向的地址不能被修改,但是该指针指向的内容可以被修改。
2.常量指针
意思:是一个指针,指向一个常量。所以称为常量指针。
表达:const int *p 或int const *p
#include <stdio.h>
int main()
{
int a,b;
const int *p=&a;
// int const *p=&a;
*p=1;//这里是错误的
p=&b;
printf("%d\n",*p);
printf("%p\n",p);
return 0;
}
结论:常量指针指向的地址可以被修改,指针指向的内容不能被修改。
3.指向常量的指针常量
表达:const int * const p
结论:指向常量的指针常量指向的地址不可以被修改,指向的内容也不能被修改。
总结
那道题我想应该会了吧!