zoukankan      html  css  js  c++  java
  • 指向const的指针和const指针

    1.指向const的指针:const int *p 或 int const *p

    解释:p是一个指针,指向const int类型的常量;指针指向的内容为常量,因此不能改变*p的值,但指针p可以改变,指向不同的const int常量

    const int a=2;
    const int b=3;
    const int *p=&a;
    *p=4;         //error,p指向常量a,不能修改
    p=&b;        //ok,p只要指向const int类型即可

    2.const指针:int *const p;

    解释:p是一个指针,是指向int类型的const指针;指针p的值不能改变,但其指向的值可以改变

    int a=2;
    int b=3;
    int *const p=&a;    
    *p=4;          //ok,p的内容可以改变
    p=&b;         //error,p是常指针,指针值不能修改

    3.指向const 的 const指针:const int *const p 或 int const *const p

    解释:p是一个const指针,p指向const int 类型的常量;指针值和指向的对象的值都不允许修改

    int a=2;
    int b=3;
    const int *const p=&a;
    *p=4;            //error,p指向常量
    p=&b;           //error,p是常指针
  • 相关阅读:
    Codeforces 1230E
    扫描线求面积和周长 (模板,线段树)
    POJ2528
    线段树 (模板)
    Codeforces 1332B
    Codeforces 1279D
    Codeforces 1281B
    Codeforces 1288D
    8I
    8F
  • 原文地址:https://www.cnblogs.com/mrlsx/p/5444358.html
Copyright © 2011-2022 走看看