zoukankan      html  css  js  c++  java
  • C++(二十) — 指针常量和常量指针

    1、const 常量概念

      对于 const 定义的常量,必须在定义时初始化,不能在程序执行运行过程中改变。

    2、指针常量、常量指针 区别  

    (1)技巧:从右向左读,替代方法:

      p:换为  p is a;

      * :换为  point to  ;

    (2)const 在  *  左边:表示,指针指向为常量,不能改变内容;

    const 在  *  右边:指针本身是常量,指针不能改变,但可以改变内容;

    // p is a point to int const, 所指存储空间内的值是一个常量,不可改变内存
        const int* p;
        int const* p;//与上面的相关
    
        // p is a const point to int, p是一个常量指针,指向int,可以改变内容,但指针本身不可以改变
        int* const p; // 必需要初始化,
    
        // p is a const point to const int,p 是常量指针,指向整型常量
        // 必需要初始化
        const int *const p;
        int const* const p;  //与上面的相关

     3、指针数组、数组指针 区别

    char c[] = "hello"; //分配局部数组
        char *c = "hello";  //指针变量是全局变量
    
        // 数组指针,指向 int 数组的指针
        int(*ptr)[];
    
        // 指针数组,数组中保存的指针地址
        int* ptr[];
        int*(ptr[]);

    4、指针

    malloc/free: 标准库函数

    new/delete:  c++运算符,动态内存分配及初始化

  • 相关阅读:
    MongoDB 与 MySQL 性能比较
    PySpider简易教程
    使用redis有什么缺点
    禅道
    Shell02
    Shell01
    性能测试06
    性能测试05
    性能测试04
    性能测试03
  • 原文地址:https://www.cnblogs.com/eilearn/p/10146271.html
Copyright © 2011-2022 走看看