辨析 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的首地址 ...
随机推荐
- k8s创建资源的两种方式(5)
一.创建方式分类: 命令 vs 配置文件 Kubernetes 支持两种方式创建资源: 1.用 kubectl 命令直接创建,比如: kubectl run httpd-app --image=reg ...
- [SOME_MUTATION] (state) {// mutate state}Vuex中使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名
使用常量替代 Mutation 事件类型 使用常量替代 mutation 事件类型在各种 Flux 实现中是很常见的模式.这样可以使 linter 之类的工具发挥作用,同时把这些常量放在单独的文件中可 ...
- 自动化运维:(1)认识 Shell
目录 (一)运维是什么? (二)什么是 Shell? (三)Shell的分类 (四)Shell脚本 (五)Shell的变量 (六)表达式 (七)Linux常见符号 (八)常见命令 (一)自动化运维是什 ...
- 【JulyEdu-Python基础】第 7 课:Python并发编程以及系统常用模块
主要内容 Python多进程与多线程 Python使用Hadoop分布式计算库mrjob Python使用Spark分布式计算库PySpark 例子:分别使用MapReduce和Spark实现word ...
- (长期更新)【python数据建模实战】零零散散问题及解决方案梳理
注1:本文旨在梳理汇总出我们在建模过程中遇到的零碎小问题及解决方案(即当作一份答疑文档),会不定期更新,不断完善, 也欢迎大家提问,我会填写进来. 注2:感谢阅读.为方便您查找想要问题的答案,可以就本 ...
- APP安全_Android反编译
反编译 Android的反编译工具:apktool,JEB等. Apk 文件的结构,如下: META-INF:签名文件 res:资源文件,里面的 xml 格式文件在编译过程中由文本格式转化为二进制的 ...
- APP安全_Android渗透环境
Android渗透 移动APP大多通过WEB API服务的方式与服务端进行交互,这种模式把移动安全和web安全绑在一起.常见的web漏洞在移动APP中也存在,比如SQL注入,文件上传,中间件/serv ...
- JS自适应导航栏,菜单栏
1. 打开 https://github.com/VPenkov/okayNav下载源代码 2.引入两个css样式 <link rel="stylesheet" href=& ...
- 前端控制台 JavaScript函数报错 SyntaxError: expected expression, got ';' SyntaxError: expected expression, got 'if'
在火狐浏览器下调试时, 页面报错SyntaxError: expected expression, got ';'或者SyntaxError: expected expression, got 'if ...
- CGI 萃取技术 __type_traits
前言 上一篇文章讲了 iterator_traits 的编程技法,非常棒适度弥补了 C++ 语言的不足. STL 只对迭代器加以规范,制定了 iterator_traits 这样的东西. CGI 把这 ...