常量指针和指针常量 C / C++

2021-06-09 00:02

阅读:656

标签:inter   out   assign   col   c++   ons   int   ann   ssi   

按英文的字面意思就比较好理解了:

常量指针:pointer to const

字面意思:指向常量的指针

 

指针常量:const pointer

字面意思:指针本身是个常量

 

看一段代码:

    char hello[6] = "hello";
    char world[6] = "world";

    const char *p2const = hello;    // pointer to const
    char *const constp = hello;     // const pointer

    p2const = world;    // value of p2const is "world"
    // constp = world;  // constant pointer cannot be reassigned

    constp[0] = y;       // value of constp is "yello"
    // p2const[0] = ‘y‘;   // pointer to constant, cannot change the value

    cout  // "world yello"    

 

常量指针和指针常量 C / C++

标签:inter   out   assign   col   c++   ons   int   ann   ssi   

原文地址:https://www.cnblogs.com/harrypotterisdead/p/14504864.html


评论


亲,登录后才可以留言!