指针常量与常量指针

这里有一篇关于利用顺时针解读的文字,按照这个方法理解了以后,再去区分指针常量与常量指针的定义应该不难。

http://c-faq.com/decl/spiral.anderson.html

例一:

    const int * p1 = &a; 
    p1 = &b; //正确
    //*p1 = 100;  报错

const修饰的是指针,指针指向可以改,指针指向的值不可以更改
按照文章中介绍的方法去理解:

  • p1 is a pointer
  • p1 is a pointer to int
  • p1 is a pointer to int which is const

例二:

    int * const p2 = &a;
    //p2 = &b; //错误
    *p2 = 100; //正确

const修饰的是常量,指针指向不可以改,指针指向的值可以更改按照文章中介绍的方法去理解:

  • p2 is const
  • p2 is const pointer
  • p2 is a const pointer to int

例三:

    const int * const p3 = &a;

const既修饰指针又修饰常量
照文章中介绍的方法去理解:

  • p3 is const
  • p3 is const pointer of int
  • p3 is const pointer of const int

当然我的英语语法不一定正确,但是就是为了说明这么个意思。有些时候,从翻译过来的字面意思理解这种问题可能真的是比较绕,那我们不妨换一下思路,用英语思维去理解,可能会好一些。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。