C语言关键字之存储类型关键字const

目录

前言

一、const关键字的含义

二、常量指针(const int *ptr)

三、指针常量:int * const ptr

四、const int * const ptr

总结


前言

C语言最重要的关键字const的介绍,包括指针常量和常量指针等介绍。


一、const关键字的含义

(1) 只读。
(2) 使用关键字const也许能产生更紧凑的代码。
(3) 使编译器很自然地保护那些不希望被改变的参数,防止其被无意的代码修改。
(4) const与指针const离谁近就修饰谁,int const *p与const int *p一样。

二、常量指针(const int *ptr)

含义:让指针指向一个常量对象,防止使用该指针来修改其所指向的值。

(declares a changeable pointer to a constant integer. The integer value cannot be changed,through this pointer, but the pointer may be changed to point to a different constant integer.)

int const *p;    //const修饰的是*p,不能改变*p
p = &a;    //正确
*p = 20;    //错误,不能通过*p改变a的值
int age = 39;
const int *pt = &age;

分析:pt的声明并不意味着它指向的值实际上就是一个常量,而只是意味着对pt而言,这个值是常量。如,*pt = 20;或 *pt += 1;就不允许,而age = 20;就允许。

三、指针常量:int * const ptr

含义:将指针本身声明为常量,防止改变指针指向的地址。
(declares a constant pointer to changeable integer data. The integer value can be changed through this pointer, but the pointer may not be changed to point to a different constant integer.)

int gorp = 16;
int chip = 12;
int *const p_snack = &gorp;    //const修饰的是p_snack
*p_snack = 20;    //正确,p_snack可以用来修改值。
p_snack = &chip;    //错误,禁止用来改变p_snack指向的变量。

四、const int * const ptr

含义:forbids changing either the address ptr contains or the value it points to.

int const * const p;
p = &b;    //不能改变p
*p = 20;    //不能改变*p


总结

提示:这里对文章进行总结:

例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值