#include "stdafx.h"
#include <iostream>



int main()
{
    //an object can not to be modified
    char dat = 0;
    const char i = 9;
    
    const char *p = &i;    //data is const,pointer is not
    p++;

    char *const p1 = &dat; //pointer is a const, dat is not a const
    (*p1)++;

    char const *p2 = &dat;  //data and pointer are both const    


    const char* const p3 = &i;    //data and pointer are both const

    return 0;
}
  • if const is on the left of *,data is const  //如果const在*左边数据就是const类型
  • if const is on the right of *,pointer is const
  • why use const
    • Guards anainst inadvertent write to the variable   //防止无意的改写数据
    • Self documenting                                                  //定义常量
    • Enables compiler to do more optimiztion, making code tighter   //使编译器做优化,使程序更加严谨
    • const means the variable can be put in the rom       //const变量意味着变量将被存到ron区域
版权声明:本文为bixiaopengblog原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/bixiaopengblog/p/8318932.html