通過實例來看看const所修飾的指針使用方法:
1、
先看一個普通的指針使用。
形式:int *pTmp
含義:一個普通的指向int類型的指針
[cpp]
int main(int argc, char *argv[])
{
int i = 1;
int *pTmp = &i;
printf("pTmp = %d\n", *pTmp);
i = 2;
printf("pTmp = %d\n", *pTmp);
(*pTmp)++;
printf("pTmp = %d\n", *pTmp);
system("PAUSE");
return 0;
}
int main(int argc, char *argv[])
{
int i = 1;
int *pTmp = &i;
printf("pTmp = %d\n", *pTmp);
i = 2;
printf("pTmp = %d\n", *pTmp);
(*pTmp)++;
printf("pTmp = %d\n", *pTmp);
system("PAUSE");
return 0;
}
輸出:
[plain]
pTmp = 1
pTmp = 2
pTmp = 3
請按任意鍵繼續. . .
pTmp = 1
pTmp = 2
pTmp = 3
請按任意鍵繼續. . .
沒有任何問題,pTmp指針你可以任意操作。
2、
形式:const int *pTmp
含義:表示pTmp所指向的對象是只讀的,但pTmp可指向其他地址,即pTmp可變。
[cpp]
int main(int argc, char *argv[])
{
int i = 1;
int j = 100;
const int *pTmp = &i; /*正確:表示pTmp所指向的對象是只讀的,但pTmp可指向其他地址,即pTmp可變*/
printf("pTmp = %d\n", *pTmp);
i = 2;
printf("pTmp = %d\n", *pTmp);
pTmp = &j; /*正確:將pTmp指向變量j的地址*/
printf("pTmp = %d\n", *pTmp);
(*pTmp)++; /*錯誤,pTmp指向的對象是只讀的,編譯出錯提示:assignment of read-only location */
printf("pTmp = %d\n", *pTmp);
system("PAUSE");
return 0;
}
int main(int argc, char *argv[])
{
int i = 1;
int j = 100;
const int *pTmp = &i; /*正確:表示pTmp所指向的對象是只讀的,但pTmp可指向其他地址,即pTmp可變*/
printf("pTmp = %d\n", *pTmp);
i = 2;
printf("pTmp = %d\n", *pTmp);
pTmp = &j; /*正確:將pTmp指向變量j的地址*/
printf("pTmp = %d\n", *pTmp);
(*pTmp)++; /*錯誤,pTmp指向的對象是只讀的,編譯出錯提示:assignment of read-only location */
printf("pTmp = %d\n", *pTmp);
system("PAUSE");
return 0;
}
3、
形式:int const *pTmp
含義:與2相同, 表示pTmp所指向的對象是只讀的,但pTmp可指向其他地址,即pTmp可變。
[cpp]
int main(int argc, char *argv[])
{
int i = 1;
int j = 100;
const int *pTmp = &i; /*正確:表示pTmp所指向的對象是只讀的,但pTmp可指向其他地址,即pTmp可變*/
printf("pTmp = %d\n", *pTmp);
i = 2;
printf("pTmp = %d\n", *pTmp);
pTmp = &j; /*正確:將pTmp指向變量j的地址*/
printf("pTmp = %d\n", *pTmp);
(*pTmp)++; /*錯誤,pTmp指向的對象是只讀的,編譯出錯提示:assignment of read-only location */
printf("pTmp = %d\n", *pTmp);
system("PAUSE");
return 0;
}
int main(int argc, char *argv[])
{
int i = 1;
int j = 100;
const int *pTmp = &i; /*正確:表示pTmp所指向的對象是只讀的,但pTmp可指向其他地址,即pTmp可變*/
printf("pTmp = %d\n", *pTmp);
i = 2;
printf("pTmp = %d\n", *pTmp);
pTmp = &j; /*正確:將pTmp指向變量j的地址*/
printf("pTmp = %d\n", *pTmp);
(*pTmp)++; /*錯誤,pTmp指向的對象是只讀的,編譯出錯提示:assignment of read-only location */
printf("pTmp = %d\n", *pTmp);
system("PAUSE");
return 0;
}
4、
形式:int * const pTmp
含義:pTmp不可修改,但pTmp所指向的對象可以修改。
[cpp]
int main(int argc, char *argv[])
{
int i = 1;
int j = 100;
int * const pTmp = &i;
printf("pTmp = %d\n", *pTmp);
i = 2;
printf("pTmp = %d\n", *pTmp);
pTmp = &j; /*錯誤,pTmp不可修改,編譯提示:error:assignment of read-only variable `pTmp'*/
printf("pTmp = %d\n", *pTmp);
(*pTmp)++; /*正確,pTmp所指向的對象可以修改 */
printf("pTmp = %d\n", *pTmp);
system("PAUSE");
return 0;
}
int main(int argc, char *argv[])
{
int i = 1;
int j = 100;
int * const pTmp = &i;
printf("pTmp = %d\n", *pTmp);
i = 2;
printf("pTmp = %d\n", *pTmp);
pTmp = &j; /*錯誤,pTmp不可修改,編譯提示:error:assignment of read-only variable `pTmp'*/
printf("pTmp = %d\n", *pTmp);
(*pTmp)++; /*正確,pTmp所指向的對象可以修改 */
printf("pTmp = %d\n", *pTmp);
system("PAUSE");
return 0;
}
5、
形式:const int * const pTmp
含義:pTmp不可修改,pTmp所指對象也不能修改。
[cpp]
int main(int argc, char *argv[])
{
int i = 1;
int j = 100;
const int * const pTmp = &i;
printf("pTmp = %d\n", *pTmp);
i = 2; /*正確*/
printf("pTmp = %d\n", *pTmp);
pTmp = &j; /*錯誤:error: assignment of read-only variable `pTmp'*/
printf("pTmp = %d\n", *pTmp);
(*pTmp)++; /*錯誤:error: increment of read-only location*/
printf("pTmp = %d\n", *pTmp);
system("PAUSE");
return 0;
}
摘自 Socrates的專欄