zoukankan      html  css  js  c++  java
  • 常量和指针(Pointers and Constants)

     

    常量和指针(Pointers and Constants
    ——const 修饰的指针解惑
     
    一般遇到用const修饰的常量涉及到指针就会比较麻烦,容易把头搞晕,有个简单的技巧就是从右向左看,下面我举例子说明:
           const int* p1 = NULL; //写法一
           int const* p2 = NULL; //写法二
        int *const p3 = NULL; //写法三
    以下的赋值语句,被注释掉并加上“//error”提示的是错误写法,其他是正确的:
           p1 = NULL;
           p2 = NULL;
    //     *p1 = 1; //error
    //     *p2 = 2; //error
    //     p3 = NULL; //error
           *p3 = 0;
    定义了3个指针,p1,p2,p3, 写法一和写法二是等价的,所以主要看p2和p3的区别,
     
    那么根据我们从右向左的技巧,就是看const的右边:
    p2:const右边是“* p2”,所定义的常量是p2所指向的常量,则p2=0正确,*p2=0错误
    p2:const右边是“p3”,所定义的常量是p3这个指针本身,则p3=0错误,*p3=0正确
  • 相关阅读:
    Hashmap实现原理
    策略模式
    Google Drive ubuntu
    numix Docky
    Google Drive 和 Dropbox 同步同一个文件夹目录
    sublime text 2
    matlab cell
    liteide
    taglist and nerdtree
    codeblocks
  • 原文地址:https://www.cnblogs.com/secbook/p/2655488.html
Copyright © 2011-2022 走看看