辨析 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的首地址 ...
随机推荐
- JPA,Hibernate,Spring Data JPA之间的关系
什么么是JPA? 全称Java Persistence API(JAVA对象持久化API),可以通过注解或者XML描述[对象-关系表]之间的映射关系,并将实体对象持久化到数据库中. 为我们提供了: 1 ...
- hadoop安装和环境配置---1
一.安装java 1.下载 : yum install java-1.7.0-openjdk 2.配置环境变量 vim /etc/profile 要先看自己安装的java全名 然后再环境配置 expo ...
- 从源码学习使用 node-delegates
node-delegates 是 TJ 大神所写的一个简单的小工具,源码只有 157 行,作用在于将外部对象接受到的操作委托到内部属性进行处理,也可以理解为讲对象的内部属性暴露到外部,简化我们所需要书 ...
- Scala中的列表可以添加元素吗?
列表或许是Scala程序中最常用到的数据结构了,其与数组非常相似,最重要的两点差别为: 1.列表是不可变的: 2.列表具有递归结构,而数组是连续的. 在实际使用中非常容易这样用: val a = Li ...
- android#编写一个聊天界面
摘自<第一行代码>——郭霖 既然是要编写一个聊天界面,那就肯定要有收到的消息和发出的消息.上一节中我们制作的message_left.9.png可以作为收到消息的背景图,那么毫无疑问你还需 ...
- python录音并调用百度语音识别接口
#!/usr/bin/env python import requests import json import base64 import pyaudio import wave import os ...
- OpenResty + Lua访问Redis,实现高并发访问时的毫秒级响应打回
一.lua中redis的配置依赖: 1.OpenResty的lua访问redis的插件:https://github.com/openresty/lua-resty-redis 二.下载后,导入对应的 ...
- [转帖]基于docker 搭建Prometheus+Grafana
基于docker 搭建Prometheus+Grafana https://www.cnblogs.com/xiao987334176/p/9930517.html need good study 一 ...
- 【转帖】.NET的一点历史故事:作者的一些感想
.NET的一点历史故事:作者的一些感想 https://mp.weixin.qq.com/s?__biz=MzAwNTMxMzg1MA==&mid=2654068684&idx=2&a ...
- Linux 防火墙设置常用指令
查看防火墙状态命令: service firewalld status systemctl status firewalld 结果: 其中: enabled:开机启动(开机不启动是disabled ...