指向常量的指針:該指針指向的值不能改變,指針不是常量,所以可以修改指針的指向
int main(void)
{
int value = 10;
/*定義一個指向常量的指針*/
const int *pValue = &value;
//pValue = &value;
/**
*
* 編譯器會報錯,*pValue是隻讀的。
* 也就是指針指向的值不能改變
*/
/**
* 這個賦值編譯器會報錯 assignment of read-only location '*pValue'
* 因爲*pValue指向的值是常量,所以不能改變。
*/
*pValue = 20;
/*但可以對value的值進行改變*/
int number = 30;
/**
* 指針不是常量,所以可以修改指針的指向
*/
pValue = &number;
return 0;
}
常量指針
#include <stdio.h>
/**
- 常量指針
-
指針中存儲的地址不能改變
/
int main(void)
{
int value = 10;
int const pValue = &value;int item = 34;
/**- 編譯器報錯,assignment of read-only variable 'pValue'。
- 說明不能修改指針中存儲的地址
/
pValue = &item;
/但是可以修改地址所指向的值/
pValue = 24;
return 0;
}