辨析以下几种指针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指针 和 指向常量的指针的更多相关文章

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

    先看下面六种写法: . const int p; . const int *p; . int const* p; . int * const p; . const int * const p; . i ...

  2. 【转】const int *p和int * const p的区别(常量指针与指向常量的指针)

    [转]作者:xwdreamer   出处:http://www.cnblogs.com/xwdreamer 对于指针和常量,有以下三种形式都是正确的: const char * myPtr = &am ...

  3. C++指向常量的指针和常指针

    C++指向常量的指针和常指针 指向常量的指针 通常情况下,可以通过指针去修改指针指向的内容.但是在某些情况下,只希望通过指针去访问指针指向的内容,不想修改.比如只想通过树根结点的指针去遍历输出树中所有 ...

  4. c++中指针常量,常指针,指向常量的常指针区分

    const char * myPtr = &char_A;//指向常量的指针 char * const myPtr = &char_A;//常量的指针 const char * con ...

  5. 常量指针-指向常量的指针,指针常量-指针本身是常量,常量-不能更改值的常量,数组指针-是指针int (*p)[n] 指针数组-是数组int *p[n]

    1.常量指针 定义:具有只能够读取内存中数据,却不能够修改内存中数据的属性的指针,称为指向常量的指针,简称常量指针. 声明:const int * p; int const * p; 注:可以将一个常 ...

  6. C 指针常量 和常量指针 指向常量的指针常量的使用

    #include <stdio.h> /* 指针常量 和常量指针 指向常量的指针常量 */ int main() { int a = 100; int b =200; int* const ...

  7. 指针常量&常量指针&指向常量的指针常量

    搞不懂不吃晚饭 (1)指针常量 指针常量是一个常量,但是是指针修饰的. 格式:int * const p; 例如 int a, b; int * const p = &a;//指针常量 //分 ...

  8. C++ 中指针常量、指向常量的指针、引用类型的常量

    命题1. 在C++ 中 const T a 与 T const a 是一样的, 表示a是一个T类型的常量. 测试: 一. 形参定义为引用类型的常量 在函数传参时,形参若定义为 const T& ...

  9. [C++]指针和指向数组的指针[一维数组与指针]

     1.一维数组与指针      形如:int型 数组 a[10]                1)&a[0]  地址常量;地址类型:int *型   ; 存储数组a的首地址          ...

随机推荐

  1. jmeter 执行python脚本的方法 。(亲测ok)

    jmeter 执行python脚本   jmeter 可以通过Jython 执:行python代码 1.下载Jython jar包:http://www.jython.org/downloads.ht ...

  2. [Spark] Scala programming - basic level

    环境配置 IDE: https://www.jetbrains.com/idea/ 子雨大数据之Spark入门教程(Scala版) /* implement */ 语言特性 Online compil ...

  3. Hive的内部表和外部表

  4. Java并发——volatile

    CPU的内存模型如下:

  5. Kubernetes web界面kubernetes-dashboard安装【h】

    本文讲述的是如何部署K8s的web UI,前提是已经有一个k8s集群后,按照如下步骤进行即可.(如下步骤都是在master节点上进行操作) 1.下载kubernetes-dashboard.yaml文 ...

  6. Unity热更新 xLua

    xLua是Unity3D下Lua编程解决方案,自2016年初推广以来,已经应用于十多款腾讯自研游戏,因其良好性能.易用性.扩展性而广受好评.现在,腾讯已经将xLua开源到GitHub. 2016年12 ...

  7. Zabbix官方部署搭建

    Zabbix在企业生产环境中是用的最广泛的服务器监控软件,其功能强大.配置简单.开源免费,是企业监控软件的首选. 一.Zabbix简介   zabbix是一个基于WEB界面的提供分布式系统监视以及网络 ...

  8. 调用存储在session属性里的东西

    将对象放在session里面 request.getSession().setAttribute("username", username); //放到会话里 永EL表达式调用 $ ...

  9. freeRTOS学习8-21

    不能再中断服务程序调用该函数 应该调用xQueueSendFromISR()

  10. Kafka性能调优分析-线上环境篇

    一.背景介绍: 在平时的开发中,使用kafka来发送数据已经非常熟悉,但是在使用的过程中,其实并没有比较深入的探索kafka使用过程中 一些参数配置,带来的损失可能就是没有充分的发挥出kfka的优势, ...