- const修饰变量:
- const关键字修饰变量时,该变量表示是一个只读变量,不能通过变量名修改变量的值;
- 案例:
const int m = 20;
int const n = 30;
m = 30;
n = 20;
- const修饰指针:
- 三种情况:
- 1.指针的指向可以修改,但是不能通过指针修改指向空间里面的内容;
- 案例:
const int *p;
int const *p;
- 2.指针的指向不可以修改,但是能通过指针修改指向空间里面的内容;
- 案例:
int * const p;
- 3.指针的指向不可以修改,也不能通过指针修改指向空间里面的内容;
- 案例:
const int * const p;
#include<stdio.h>int main(int argc, const char *argv[])
{int const m = 20;const int n = 30;printf("m = %d,n = %d\n",m,n);puts("");int a = 11;int b = 22;int c = 33;int d = 44;const int *p = &a;int const *q = &b;printf("*p = %d,*q = %d\n",*p,*q);p = &c;q = &d;printf("a = %d,b = %d\n",a,b);printf("*p = %d,*q = %d\n",*p,*q);puts("");int e = 55;int * const k = &e;printf("e = %d\n",e);*k = 66;printf("e = %d,*k = %d\n",e,*k);puts("");int i = 77;const int * const s = &i;printf("i = %d, *s = %d\n",i,*s);return 0;
}
m = 20,n = 30*p = 11,*q = 22
a = 11,b = 22
*p = 33,*q = 44e = 55
e = 66,*k = 66i = 77, *s = 77