const keyword usage --C language

First, conventional usage

Const keyword is used to define a read-only variable, the variable is const defined its value is not allowed to change, which does not allow it to be reassigned, even if you can not assign the same value. So it is read-only variables defined, which means that it must give initial value at the time of definition.

Const variables with the modified format is usually

1 const type name = value;

Code Example; (a first embodiment)

1 const int Max;

This can also be written below (second way)

1  int  const Max;

The first way to use under normal circumstances (const variables will be recommended modification of the first letter capitalized), once they are modified const variable is created and its value can not be changed, it must be assigned constants (initialization) defined at the same time, any behavior behind the assignment will throw an error.

Error code examples:

 

Two, const pointer

const conjunction with pointer has two functions, one limit pointer variable, two pointer variables to limit data points

Restrictions pointer variable itself

1  int * const P2; // const pointer variable is modified

Pointer variable restriction means itself, the value of the pointer variable itself can not be modified, so that the pointer variable is a pointer const modified only initialized when defined, then after the assignment can not be defined, the following error code

Limit pointer variable data points

1 const int *p1;
2 int const *p1;

上面两种写法都可以,一般使用第一种,限制指针变量指向的数据的意思就是指针可以指向不同的变量(指针本身的值可以修改),但是不能用指针修改指针指向的数据的值,错误代码如下

区分const是限制的指针变量还是指针变量指向数据的值:const 离变量名近就是用来修饰指针变量的,离变量名远就是用来修饰指针指向的数据,如果近的和远的都有,那么就同时修饰指针变量以及它指向的数据。

当然也可以同时限制指针变量和指针变量指向的数据的值,写法如下

1 const int * const p2;

上面这种写法使指针变量和指针变量指向数据的值都不能修改

 

三、const和函数形参

在很多情况下,const修饰的变量完全可以使用 #define命令代替,const 通常用在函数形参中,在C标准库中有很多函数形参都用const限制了,为了防止在函数内部修改指针指向的数据,例如 fopen_s

 

四、const和非const类型的转换

当一个指针变量 str1 被 const 限制时,并且类似const char *str1这种形式,说明指针指向的数据不能被修改;如果将 str1 赋值给另外一个未被 const 修饰的指针变量 str2,就有可能发生危险。

因为通过 str1 不能修改数据,而赋值后通过 str2 能够修改数据了,意义发生了转变,所以编译器不提倡这种行为,会给出错误或警告,如下图

也就是说,const char *char *是不同的类型,不能将const char *类型的数据赋值给char *类型的变量。但反过来是可以的,编译器允许将char *类型的数据赋值给const char *类型的变量。这种限制很容易理解,char *指向的数据有读取和写入权限,const char *指向的数据只有读取权限,降低数据的权限不会带来任何问题,但提升数据的权限就有可能发生危险。

上面这种情况是编译器是允许的

 

五、const与#define

1、define是预编译指令,而const是普通变量的定义。define定义的宏是在预处理阶段展开的,而const定义的只读变量是在编译运行阶段使用的。

 

2、const定义的是变量,而define定义的是常量。define定义的宏在编译后就不存在了,它不占用内存,因为它不是变量,系统只会给变量分配内存。但const定义的常变量本质上仍然是一个变量,

具有变量的基本属性,有类型、占用存储单元。可以说,常变量是有名字的不变量,而常量是没有名字的。有名字就便于在程序中被引用,所以从使用的角度看,除了不能作为数组的长度,

用const定义的常变量具有宏的优点,而且使用更方便。所以编程时在使用const和define都可以的情况下尽量使用常变量来取代宏。

 

3、const定义的是变量,而宏定义的是常量,所以const定义的对象有数据类型,而宏定义的对象没有数据类型。所以编译器可以对前者进行类型安全检查,而对后者只是机械地进行字符替换,

没有类型安全检查。这样就很容易出问题,即“边际问题”或者说是“括号问题”。

Guess you like

Origin www.cnblogs.com/lanhaicode/p/11184505.html