zoukankan      html  css  js  c++  java
  • const和typedef的用法

        无意间看待同学的一篇关于const究竟修饰了谁的文章,里面有一些关于typedef的用法,现在贴出源代码

    #include<stdio.h>

    typedef int* IntPointer;

    int main()
    {
     int value=10;
     const int *pA=&value;
     pA++;
         //*pA=11;编译错误
     const IntPointer pB=&value; 
     *pB=12;
     //pB++;//编译错误
     return 0;
    }

    *pA=11编译错误很容易理解,因为const修饰使得内存值为只读,就是指向一个常量。

    pB++编译错误,却很难理解,因为此时IntPointer就相当于int *,那么根据定义pB只是一个普通指针,不是常量指针,那么为什么不能pB++呢?

    原因在于const Intpointer pB其实不相当于const int *pB,它其实和const int pB没什么区别,

    只不过此处变量p2的数据类型是我们自己定义的而不是系统固有类型而已,因此const IntPointer pB的含义是:限制数据类型为int *的变量pB为只读,所以pB++错误.

  • 相关阅读:
    053-146
    053-659
    053-658
    053-657
    053-656
    053-654
    053-655
    053-652
    053-651
    053-650
  • 原文地址:https://www.cnblogs.com/fightingxu/p/2807242.html
Copyright © 2011-2022 走看看