辨析 const指针 和 指向常量的指针
辨析以下几种指针p的定义。
int tmp = 5;int *p = &tmp; const int *p = &tmp; int const* p = &tmp; int * const p = &tmp; const int * const p = &tmp; int const * const p = &tmp;
根据文献一,可以采用从右往左读的方式区分。
第一个为普通指针,指向普通int变量;
第二个和第三个相同,都是普通指针,指向const int型变量;
第四个是const指针,指向普通int变量;
第五个和第六个相同,都是const指针,指向const int型变量。
实验代码如下:
1 #include <iostream> 2 3 4 void test1() { 5 int tmp = 5; 6 int *p = &tmp; 7 std::cout << "test1:: p value: " << *p << std::endl; 8 // *p and p are common variables. 9 *p = 10; // ok 10 int tmp1 = 9; 11 p = &tmp1; // ok 12 } 13 14 void test2() { 15 int tmp = 5; 16 const int *p = &tmp; 17 std::cout << "test2:: p value: " << *p << std::endl; 18 // *p is read-only, p is common variable. 19 // *p = 10; // error 20 int tmp1 = 9; 21 p = &tmp1; // ok 22 } 23 24 // same with test2 25 void test3() { 26 int tmp = 5; 27 int const* p = &tmp; 28 std::cout << "test3:: p value: " << *p << std::endl; 29 // *p is read-only, p is common variable. 30 // *p = 10; // error 31 int tmp1 = 9; 32 p = &tmp1; // ok 33 } 34 35 void test4() { 36 int tmp = 5; 37 int * const p = &tmp; 38 std::cout << "test4:: p value: " << *p << std::endl; 39 // p is read-only, *p is common variable. 40 *p = 10; // ok 41 // int tmp1 = 9; 42 // p = &tmp1; // error 43 } 44 45 void test5() { 46 const int tmp = 5; 47 const int * const p = &tmp; 48 std::cout << "test5:: p value: " << *p << std::endl; 49 // p is read-only, *p is also read-only. 50 // *p = 10; // error 51 // int tmp1 = 9; 52 // p = &tmp1; // error 53 } 54 55 56 // same with test5 57 void test6() { 58 const int tmp = 5; 59 int const * const p = &tmp; 60 std::cout << "test6:: p value: " << *p << std::endl; 61 // p is read-only, *p is also read-only. 62 // *p = 10; // error 63 // int tmp1 = 9; 64 // p = &tmp1; // error 65 } 66 67 int main() { 68 std::cout << "Hello, World!" << std::endl; 69 test1(); 70 test2(); 71 test3(); 72 test4(); 73 test5(); 74 test6(); 75 return 0; 76 }
References:
(1) https://www.cnblogs.com/bencai/p/8888760.html
转载于:https://www.cnblogs.com/tlz888/p/11350521.html
辨析 const指针 和 指向常量的指针相关推荐
- const指针和指向常量的指针
先看下面六种写法: 1. const int p;2. const int *p;3. int const* p;4. int * const p;5. const int * const p;6. ...
- char * const p; //常量指针,p的值不可以修改 char const * p;//指向常量的指针,指向的常量值不可以改 const char *p; //和char const *p
char * const p; //常量指针,p的值不可以修改 char const * p://指向常量的指针,指向的常量值不可以改 const char *p: //和char const *p
- [C++基础]018_常量指针和指向常量的指针
先来看一下什么是常量指针,什么是指向常量的指针吧! 1. 常量指针定义 1 int * const ptr = new int(); 2. 指向常量的指针 1 const int* ptr; 上面已经 ...
- 常指针和指向常量的指针
最近在学习引用,有一些学习心得,记录如下: 1:引用是变量的别名 int a; int b&=a; 要注意引用只有声明,而没有定义,并且引用必须在声明的时候进行初始化,一旦与某个变量关联起来, ...
- 【转】const int *p和int * const p的区别(常量指针与指向常量的指针)
[转]作者:xwdreamer 出处:http://www.cnblogs.com/xwdreamer 对于指针和常量,有以下三种形式都是正确的: const char * myPtr = &am ...
- 常量指针与指向常量的指针
这两个概念经常很容易混淆,下面简单分析一下 1.常量指针: int * const p : const p 就是一个常量,然后再看*,可以看出是一个常量类型的指针,即int类型的常量指针.不能修改 ...
- 常指针、指向常量的指针、指向常量的常指针
三者的区分 简而言之- 常指针:地址不可变,内容可变 指向常量的指针:地址可变,内容不可变 指向常量的常指针:地址不可变,内容不可变 样例 代码如下: #include <iostream> ...
- 常量指针、指针常量以及指向常量的指针常量
三个名词虽然非常绕嘴,不过说的非常准确.用中国话的语义分析就可以很方便地把三个概念区分开. 一)常量指针. 常量是形容词,指针是名词,以指针为中心的一个偏正结构短语.这样看,常量指针本质是指针,常量 ...
- [C++] 指向常量的指针 VS 指针类型的常量
指向常量的指针 VS 指针类型的常量 const 修饰指针时的位置不同,作用也不相同. 1. 指向常量的指针 不能通过指向常量的指针改变所指对象的值,但指针本身可以改变,可以指向另外的对象. 例: i ...
最新文章
- java jvm调优面试题_【Java面试题第一期】有没有jvm调优经验?调优方案有哪些?...
- 如何快速下载maven依赖jar包
- java 打包成服务_maven javaProject打包发布成服务
- OpenCV相机位移引起的单应性的实例(附完整代码)
- java面试题-精心准备
- Matlab中的logspace函数,matlab之logspace函数
- BugkuCTF-PWN题pwn1-瑞士军刀
- GPT语言模型:通过生成式预训练改善语言理解 OpenAI 2018
- 外连接OUTER JOIN(三十五)
- SQL:postgresql中将时间戳转换为字符串
- 弃用 Notepad++ 还有更牛逼的选择
- Android6.0之AMS如何启动app中篇之Task的管理
- 强化学习: Q-learning实例python实现
- Adobe Bridge 2020新增功能
- tolua unity 报错_Unity3D热更新技术点——ToLua(上)
- ip地址和域名的关系是什么?
- 树莓派添加开机自启动
- 软件安全需求、设计、测试【归纳】
- 游戏是怎么赚钱的 - 聊聊山寨与混搭
- Android 解析软件包时出现问题
热门文章
- 隐藏linux操作系统版本信息,linux centos 如何查看操作系统版本信息?
- 【51单片机快速入门指南】7:片上EEPROM
- vim使用—实现程序的自动补齐(C语言)
- C#和C++结构体Socket通信
- C程序中如何获取shell命令执行结果和返回值
- React开发(273):异步调用的方式
- 重学java基础第九课:软件和软件关系
- 前端学习(3327):闭包的形式2
- 前端学习(3001):vue+element今日头条管理--项目初始化总结
- [html] websocket和http2有什么区别?http2能取代websocket吗?为什么?