C语言备忘:
- #include<stdio.h>
- int main()
- {
- int _a = 1;
- int _b = 2;
- int _c = 3;
- const int* a = &_a;
- int* const b = &_b;
-
- //*a = 5;//ssignment of read-only location ‘*a’
- a = &_c;
- *b = 5;
- //*b = &_c;//ssignment makes integer from pointer without a cast
- printf("*a = %d\n",*a);
- printf("*b = %d\n",*b);
- }
主要记录两种最常见写法:
(1)const int* a
指针a指向的地址里面内容不可变(只读),但是指针a可以指向其他的地址;
//*a = 5;//ssignment of read-only location ‘*a’改变地址里面内容就会报错
a = &_c; //修改指针指向地址木有问题
(2)int* const b
指针b指向地址不可变,但是指针b指向地址里面内容可以修改
*b = 5; //修改指针b指向地址里面内容木有问题
//*b = &_c;//ssignment makes integer from pointer without a cast 修改指针b指向地址报错
阅读(1435) | 评论(0) | 转发(0) |