指针与const
1,int *const a=&i
一旦得到某個變量的地址,不能再指向其他變量。
a的值不能被改變,也就是i的地址不能被改變???????
a++ (X)
*a=26 //OK
2,const int* p=&i
表示不能通過這個指針修改變量的值
*p=26(X)
p=&j //OK
i=27? //OK
判斷指針與const屬于何種關系,根據const在 * 前后判斷
const int *p1=&i? // 不能通過指針改變變量的值
int const *p2=&i??// 不能通過指針改變變量的值
int *const p3=&i? //?i的地址不能被改變
3,const數組?
const int [ ]={.....}
- 每個單元元素都是const
- 只能在初始化的時候給數組賦值
為了不想讓數組不被破壞,參數傳遞時,傳入const數組
f(const int [ ],length);
4,數組被看作const指針變量?還是數組就是const指針變量?
#include <stdio.h>
int main(){int b[]={1,2,3,4,5,6,7};int *const a=b;printf("b = %d\n",sizeof(b));printf("a = %d",sizeof(a));return 0;
} 輸出:
b = 28
a = 8
求他們的字節大小,發現數組和const指針變量不一樣。
所以,數組可以以被看作const變量,但不是const變量
?
總結
- 上一篇: C语言指针(就做个笔记)
- 下一篇: 11年的雪铁龙后保险杠多少钱?