C 語言中常量指標是什麼意思?
指標地址的值為常量,這意味著我們不能更改指標所指地址的值。
常量指標宣告如下 −
Data_Type const* Pointer_Name;
例如,int const *p// 指向常量整數的指標
示例
以下是 C 程式,用於說明指向常量的指標 −
#include<stdio.h>
int main(void){
int var1 = 100;
// pointer to constant integer
const int* ptr = &var1;
//try to modify the value of pointed address
*ptr = 10;
printf("%d
", *ptr);
return 0;
}輸出
當執行上述程式時,將產生以下結果 −
Display error, trying to change the value of pointer to constant integer
示例
以下 C 程式演示了去掉 const 的效果 −
#include<stdio.h>
int main(void){
int var1 = 100;
// removed the pointer to constant integer
int* ptr = &var1;
//try to modify the value of pointed address
*ptr = 10;
printf("%d
", *ptr);
return 0;
}輸出
當執行上述程式時,將產生以下結果 −
10
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP