首页 > 编程语言 >C++笔记-指针

C++笔记-指针

时间:2023-03-07 17:02:42浏览次数:50  
标签:value const int 笔记 C++ pointer ptr 指针

1. const指针和指向const的指针

  • 指向const的指针是在类型前加星号
    可以指向非const类型
    指针可以改变指向
    dereference不能改变值
  • const指针是在类型后面加星号
    指针不可以改变指向
    dereference可以改变值

参考learncpp的内容
To summarize, you only need to remember 4 rules, and they are pretty logical:

  • A non-const pointer can be assigned another address to change what it is pointing at
  • A const pointer always points to the same address, and this address can not be changed.
  • A pointer to a non-const value can change the value it is pointing to. These can not point to a const value.
  • A pointer to a const value treats the value as const when accessed through the pointer, and thus can not change the value it is pointing to. These can be pointed to const or non-const l-values (but not r-values, which don’t have an address)
    Keeping the declaration syntax straight can be a bit challenging:
  • The pointer’s type defines the type of the object being pointed at. So a const in the type means the pointer is pointing at a const value.
  • A const after the asterisk means the pointer itself is const and it can not be assigned a new address.
int main()
{
    const int x{ 5 };
    const int* ptr { &x }; // ptr points to const int x

    const int y{ 6 };
    ptr = &y; // okay: ptr now points at const int y

    return 0;
}
// 指向const的指针
int main()
{
    const int x{ 5 };
    const int* ptr { &x }; // ptr points to const int x

    const int y{ 6 };
    ptr = &y; // okay: ptr now points at const int y

    return 0;
}
// const 指针
int main()
{
    int x{ 5 };
    int y{ 6 };

    int* const ptr { &x }; // okay: the const pointer is initialized to the address of x
    ptr = &y; // error: once initialized, a const pointer can not be changed.

    return 0;
}

2. 引用和指针的type deduction(追加)

标签:value,const,int,笔记,C++,pointer,ptr,指针
From: https://www.cnblogs.com/zhaobangliu/p/17188667.html

相关文章

  • C++笔记-static本地变量
    static本地变量只能被本地看到,所以不同函数之间的static变量相同也没事,但是同一个函数调用多次会忽略后面的初始化。#include<iostream>voidmyStaticFunction(){......
  • c++笔架-编译,头文件,链接
    编译是按任意顺序进行的,并且每个文件是独立编译的,所以如果不用头文件的话,其他文件中定义的函数,在当前文件中是不可见的。也就是说没有定义。头文件只包含declaration,不包......
  • C++笔记-函数指针
    函数指针语法://fcnPtrisapointertoafunctionthattakesnoargumentsandreturnsanintegerint(*fcnPtr)();特点:函数指针的类型(参数和返回值)都必须和......
  • C++ primer 智能指针的陷阱
    1.不使用相同的内置指针值初始化(或reset)多个智能指针有一个现成的约定是当我们将一个原生指针交给(具有资源所有权的)智能指针的时候,这个智能指针被允许认为自己暂时获得......
  • python操作pandas的笔记
    importpandasaspddata={'name':['Alice','Bob','Charlie','David'],'age':[25,30,35,40],'gender':['F','M','M','M'......
  • 【双指针】LeetCode 88. 合并两个有序数组
    题目链接88.合并两个有序数组思路看到题目的第一感觉就是用双指针进行原地合并,但是nums1的元素都在数组头部,如果正序合并的话非常容易破坏nums1的结构。nums1在......
  • C++中的静态多态和动态多态
    今天学习C++时,发现C++中存在静态多态和动态多态静态多态=>也称为编译期多态=>基于模板编程的静态多态动态多态=>也称为运行期多态 =>面向对象的动态多态,它基......
  • 用openpyxl操作excel学习笔记
    课程来源:https://www.bilibili.com/video/BV19p4y1z7rM/?p=3&spm_id_from=pageDriver&vd_source=5c65398a0f1ade31116f35fc9c0cf651fromopenpyxlimportload_workbookwb......
  • Linux使用DataX3.0 ~笔记-持续更新中
     一、Liunx上安装DataX查看当前系统版本查看系统的名称,命令:uname查看具体的系统版本,命令:cat/etc/redhat-release查看是否具备jdk和python环境:查看jdk版本,命......
  • react业务开发笔记1
    自定义table空数据import{ConfigProvider,Table,}from'antd'//定义空数据展示constrenderEmpty=()=>(<EmptyimageStyle={{he......