zoukankan      html  css  js  c++  java
  • the difference between const int *, int * const, int const *

      Some people may be confused about the sequence of const and * on declaration in C++/C, me too.

      Now I think we can distinguish them by this way:

      1.only noticing the position of const to *, and we can find that the following statements are same:    

    const int * foo_int;
    int const * foo_int_;  //same.

      2.regarding const as a postpositive attributes,and you will know the content which is const.

      

    int foo = 6;
    int foo_ = 7;
    
    //the foo_int is const, but the pointer to foo_int can be chaged.
    int const * foo_int = &foo;
    
    //illegal, the value cannot be chaged
    //*foo_int = 7
    
    //okay!
    foo_int = &foo_;
    
    //the pointer to int is const, but foo_int_ can be chaged.
    int * const foo_int_ = &foo_int;
    
    //illegal, the pointer cannot be changed.
    //foo_int = &foo_int_;
    //even if the new pointer is same, it's illegal
    //foo_int = &foo_int;
    
    //okay
    *foo_int = 8;
  • 相关阅读:
    第二阶段团队冲刺第五天
    第二阶段冲刺七
    第二阶段冲刺六
    第二阶段冲刺五
    第二阶段冲刺四
    冲刺第二阶段三
    冲刺第二阶段二
    冲刺第二阶段 一
    项目总结
    第二阶段SCRUM
  • 原文地址:https://www.cnblogs.com/maikaze/p/6134361.html
Copyright © 2011-2022 走看看