读书人

const用以指针

发布时间: 2013-03-27 11:22:42 作者: rapoo

const用于指针

4)const int * const p无论指针本身还是它所指向的值都是常量,不能修改#include <stdio.h>int main(){    int b = 5;    int c = 3;    const int * const p = &b;    printf("*p is %d\n",*p);    *p = 6;//错误,向只读位置 *p赋值    p = &c;//错误,向只读变量p赋值    printf("*p is %d\n",*p);    return 0;}

简单的辨别这几种用法的判断方法

沿着*号一条线,如果const位于*左侧,那么const就是用来修饰指针所指向的变量,即指针指向常量;

如果const位于*右侧,那么const就是用来修饰指针本身,即指针本身是常量。


这样是不是很好的就知道了const到底修饰谁了呢?大家也可以试试哈 ^_^

读书人网 >编程

热点推荐