const的作用

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,有什么规律还请读者自己总结!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值