const是C语言的一个关键字,它限定一个变量不允许被改变。使用const在一定程度上可以提高程序的健壮性和减少内存占用。此外,在查看代码的时候,能够清晰理解const所起的作用,给对方理解程序有一些帮助。const最常用的是用来定义常量和常量指针,最近在网上搜了很多关于const的帖子,众说纷纭,越看越迷,我也辩别不出来那家说得对,那家说的错,于是针对使用const关键字来定义常量和常量指针从真机上测试一下:
1.用const定义常量有两种:
const int value;
int const value;
这个没有什么可争议的,让人迷惑也最让人产生分歧的是在常量指针的定义上。
2.用const定义常量指针。
1).const int *constTest使用测试代码如下:
int ValueA = 100;
int ValueB = 99;
const int *ConstTest = &ValueB;//ConstTest可变,指向的内容不可变
printf("\nConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
ConstTest = &ValueA;
printf("ConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
结果为:
ConstInt point:0x7fff5fbff7c0, ConstInt = 99
ConstInt point:0x7fff5fbff7c4, ConstInt = 100
说明ConstTest可变,而
int ValueA = 100;
int ValueB = 99;
const int *ConstTest = &ValueB;//ConstTest可变,指向的内容不可变
printf("\nConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
// ConstTest = &ValueA;
*ConstTest = ValueA;//这儿提示:Read-only variable is not assignable
printf("ConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
说明ConstTest指向的内容不可变。下面情况也类似,读者可以复制代码测试。
int ValueA = 100;
int ValueB = 99;
// const int *ConstTest = &ValueB;//ConstTest可变,指向的内容不可变
// const int* ConstTest = &ValueB;//ConstTest可变,指向的内容不可变
// const int * ConstTest = &ValueB;//ConstTest可变,指向的内容不可变
// int const *ConstTest = &ValueB;//ConstTest可变,指向的内容不可变
// int const * ConstTest = &ValueB;//ConstTest可变,指向的内容不可变
// int const* ConstTest = &ValueB;//ConstTest可变,指向的内容不可变
int const* ConstTest = &ValueB;//ConstTest可变,指向的内容不可变
printf("\nConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
ConstTest = &ValueA;
// *ConstTest = ValueA;
printf("ConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
2).int* const constTest使用测试代码如下:
int ValueA = 100;
int ValueB = 99;
int* const ConstTest = &ValueB;//ConstTest不可变,指向的内容可变
printf("\nConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
// ConstTest = &ValueA;
*ConstTest = ValueA;
printf("ConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
结果为:
ConstInt point:0x7fff5fbff7c0, ConstInt = 99
ConstInt point:0x7fff5fbff7c0, ConstInt = 100
说明ConstTest指向的内容可变,而
int ValueA = 100;
int ValueB = 99;
int* const ConstTest = &ValueB;//ConstTest不可变,指向的内容可变
printf("\nConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
ConstTest = &ValueA;//这儿提示:Read-only variable is not assignable
// *ConstTest = ValueA;
printf("ConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
说明ConstTest不可变。类似的还有
int ValueA = 100;
int ValueB = 99;
// int * const ConstTest = &ValueB;//ConstTest不可变,指向的内容可变
int *const ConstTest = &ValueB;//ConstTest不可变,指向的内容可变
printf("\nConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
ConstTest = &ValueA;
// *ConstTest = ValueA;
printf("ConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
3).const int* const ConstTest; ConstTest指针不可变,指针向的内容也不可变,与之相同的还有
int ValueA = 100;
int ValueB = 99;
// const int *const ConstTest = &ValueB;//ConstTest不可变,指向的内容不可变
const int * const ConstTest = &ValueB;//ConstTest不可变,指向的内容不可变
printf("\nConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
ConstTest = &ValueA;//这儿提示:Read-only variable is not assignable
*ConstTest = ValueA;//这儿提示:Read-only variable is not assignable
printf("ConstInt point:%p, ConstInt = %d\n", ConstTest, *ConstTest);
以上是个人所测,使用工具为XCode,有什么规律还请读者自己总结!