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