辨析 const指针 和 指向常量的指针
辨析以下几种指针p的定义。
int tmp = ; 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型变量。
实验代码如下:
#include <iostream> void test1() {
int tmp = ;
int *p = &tmp;
std::cout << "test1:: p value: " << *p << std::endl;
// *p and p are common variables.
*p = ; // ok
int tmp1 = ;
p = &tmp1; // ok
} void test2() {
int tmp = ;
const int *p = &tmp;
std::cout << "test2:: p value: " << *p << std::endl;
// *p is read-only, p is common variable.
// *p = 10; // error
int tmp1 = ;
p = &tmp1; // ok
} // same with test2
void test3() {
int tmp = ;
int const* p = &tmp;
std::cout << "test3:: p value: " << *p << std::endl;
// *p is read-only, p is common variable.
// *p = 10; // error
int tmp1 = ;
p = &tmp1; // ok
} void test4() {
int tmp = ;
int * const p = &tmp;
std::cout << "test4:: p value: " << *p << std::endl;
// p is read-only, *p is common variable.
*p = ; // ok
// int tmp1 = 9;
// p = &tmp1; // error
} void test5() {
const int tmp = ;
const int * const p = &tmp;
std::cout << "test5:: p value: " << *p << std::endl;
// p is read-only, *p is also read-only.
// *p = 10; // error
// int tmp1 = 9;
// p = &tmp1; // error
} // same with test5
void test6() {
const int tmp = ;
int const * const p = &tmp;
std::cout << "test6:: p value: " << *p << std::endl;
// p is read-only, *p is also read-only.
// *p = 10; // error
// int tmp1 = 9;
// p = &tmp1; // error
} int main() {
std::cout << "Hello, World!" << std::endl;
test1();
test2();
test3();
test4();
test5();
test6();
return ;
}
References:
(1) https://www.cnblogs.com/bencai/p/8888760.html
辨析 const指针 和 指向常量的指针的更多相关文章
- const指针和指向常量的指针
先看下面六种写法: . const int p; . const int *p; . int const* p; . int * const p; . const int * const p; . i ...
- 【转】const int *p和int * const p的区别(常量指针与指向常量的指针)
[转]作者:xwdreamer 出处:http://www.cnblogs.com/xwdreamer 对于指针和常量,有以下三种形式都是正确的: const char * myPtr = &am ...
- C++指向常量的指针和常指针
C++指向常量的指针和常指针 指向常量的指针 通常情况下,可以通过指针去修改指针指向的内容.但是在某些情况下,只希望通过指针去访问指针指向的内容,不想修改.比如只想通过树根结点的指针去遍历输出树中所有 ...
- c++中指针常量,常指针,指向常量的常指针区分
const char * myPtr = &char_A;//指向常量的指针 char * const myPtr = &char_A;//常量的指针 const char * con ...
- 常量指针-指向常量的指针,指针常量-指针本身是常量,常量-不能更改值的常量,数组指针-是指针int (*p)[n] 指针数组-是数组int *p[n]
1.常量指针 定义:具有只能够读取内存中数据,却不能够修改内存中数据的属性的指针,称为指向常量的指针,简称常量指针. 声明:const int * p; int const * p; 注:可以将一个常 ...
- C 指针常量 和常量指针 指向常量的指针常量的使用
#include <stdio.h> /* 指针常量 和常量指针 指向常量的指针常量 */ int main() { int a = 100; int b =200; int* const ...
- 指针常量&常量指针&指向常量的指针常量
搞不懂不吃晚饭 (1)指针常量 指针常量是一个常量,但是是指针修饰的. 格式:int * const p; 例如 int a, b; int * const p = &a;//指针常量 //分 ...
- C++ 中指针常量、指向常量的指针、引用类型的常量
命题1. 在C++ 中 const T a 与 T const a 是一样的, 表示a是一个T类型的常量. 测试: 一. 形参定义为引用类型的常量 在函数传参时,形参若定义为 const T& ...
- [C++]指针和指向数组的指针[一维数组与指针]
1.一维数组与指针 形如:int型 数组 a[10] 1)&a[0] 地址常量;地址类型:int *型 ; 存储数组a的首地址 ...
随机推荐
- R语言与概率统计(四) 判别分析(分类)
Fisher就是找一个线L使得组内方差小,组间距离大.即找一个直线使得d最大. ####################################1.判别分析,线性判别:2.分层抽样 #inst ...
- 删除ELK的索引
终于找到一个工具,curator,可以搜索相关信息. 记录一下: 1,查询索引: curator_cli --host 10.2.16.191 --port 9200 show_indices - ...
- PHP非对称加密
加密的类型: 在日常设计及开发中,为确保数据传输和数据存储的安全,可通过特定的算法,将数据明文加密成复杂的密文.目前主流加密手段大致可分为单向加密和双向加密. 单向加密:通过对数据进行摘要计算生成密文 ...
- 启动hive,提示ls: 无法访问/home/software/spark-2.0.1-bin-hadoop2.7/lib/spark-assembly-*.jar: 没有那个文件或目录
原因是:spark升级到spark2以后,原有lib目录下的大JAR包被分散成多个小JAR包,原来的spark-assembly-*.jar已经不存在,所以hive没有办法找到这个JAR包. 解决办法 ...
- SpringCloud学习(一)服务的注册与发现Eureka(Finchley版本)
创建服务注册中心 在这里,我还是采用Eureka作为服务注册与发现的组件. 首先创建一个空项目 首先创建一个空项目,再创建一个maven项目,首先创建一个主Maven工程,在其pom文件引入依赖,sp ...
- SpringBoot消息队列之-rabbitMQ
一.概述 1.在大多应用中,我们系统之间需要进行异步通信,即异步消息. 2.异步消息中两个重要概念:消息代理(message broker)和目的地(destination) 当消息发送者发送消息以后 ...
- 蓝鲸智云安装proxy和p-agent过程记录
1.agent_setup_pro.sh: no such file or directory 2.参考:https://bk.tencent.com/s-mart/community/questio ...
- Angular中ngx-image-cropper图片裁剪的使用
GitHub示例源码地址:https://github.com/luoruiemail/ngx-image-cropper 下载下来之后,执行yarn install安装相关node_modules包 ...
- SpringBoot中使用aop-测试
面向切面编程(AOP),该种方式主要是为了弥补面向对象编程(OOP)的不足,通过配置切面以及关注点.通知等我们可以在程序的任意位置对我们的代码进行增强(执行一些代码),AOP是Spring的特性之一, ...
- C++中如何调用DLL文件
一.动态链接库简介 动态库链接库英文位DLL,是Dynamic Link Library的缩写形式,DLL不是可执行文件.动态链接提供了一种方法,使进程可以调用不属于其可执行文件代码的函数.函数可执行 ...