目录
前言
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提供了大量能使我们快速便捷地处理数据的函数和方法。