const修飾指標與修飾常量

2021-09-29 03:56:23 字數 675 閱讀 9382

1.const 修飾指標 ——常量指標

eg:const int *p=&a;

特點:指標的指向可以修改,但是指標指向的值不能修改

int a=10;

int b=20;

int*p=

&a;*p=20;

(錯誤)

p=&b;

(正確)

2.const 修飾常量 ——指標常量

eg: int * const p=&a;

特點:指標的指向不能修改,但是指標指向的值可以修改

int a=10;

int b=20;

int*p=

&a;*p=20;

(正確)

p=&b;

(錯誤)

3.const既修飾指標又修飾常量

eg:const int * const p=&a;

特點:指標的指向和指標指向的值都不能修改

int a=10;

int b=20;

int*p=

&a;*p=20;

(錯誤)

p=&b;

(錯誤)

const修飾指標

1.指向const資料的非const指標 const int countptr 這個宣告從左到右讀,countptr 是乙個指向整數常量的指標 2.指向非const資料的const指標 int const ptr x 這個ptr指標就是const指標,宣告為const的指標必須在宣告時進行初始化。指...

const修飾指標

書寫形式為 int countptr 特點 指標的指向可以被修改,指向的資料可以被修改 includeint main 書寫形式為 const int countptr 特點 指標的指向可以被修改,指向的資料不能被修改 includevoid func const int int main void...

const用法之修飾指向常量的指標

在xp平台使用vc 6.0編譯執行如下 1 char s aaa 2 printf s n s 編譯執行一切正常,但如果在加入一些 例如 1 char s aaa 2 printf s n s 3 s 0 b 4 printf s n s 編譯通過,但執行過程中輸出 aaa 之後,系統彈出 帶除錯的...