本文只是一篇学习笔记,是看了《彻底搞定C指针》中的相关篇幅后的一点总结,仅此而已!

一、先搞清const int *p与int const *p的区别

它们的区别就是:没有区别!!

无论谁在前面都没有影响!所以const int *p与int const *p用法一样!

二、const int *p的用法

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(int argc, char **argv)
  5. {
  6. int test1 = 1;
  7. int test2 = 2;
  8. const int *p;
  9. p = &test1;
  10. p = &test2;
  11. test2 = 3;
  12. //*p = 4;     error: assignment of read-only location ‘*p’
  13. printf("%d\n", *p);
  14. return 0;
  15. }

执行结果 :3 ,这个好理解,如果加入被我注释掉的那一行就会报错,编译通不过,我用的是gcc version 4.4.3。也就是说*p是常量,不可更改,但指针p还是变量,你想怎么 变都可以。

三、int *const p的用法

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(int argc, char **argv)
  5. {
  6. int test1 = 1;
  7. int test2 = 2;
  8. int *const p = &test1;  //只能在声明的时候就给它赋初值,否则还是会报错的
  9. //p = &test2;           error: assignment of read-only location ‘*p’
  10. test1 = 3;
  11. printf("%d\n", *p);
  12. return 0;
  13. }

执行结果 :3 ,这样用p是常量,也就是说p所指向的地址是不可以更改的,所以当把test2的地址赋值给p时就会报错!但是p所指的地址内容是可以改变的。

三、补充const int *const p

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main(int argc, char **argv)
  5. {
  6. int test1 = 1;
  7. int test2 = 2;
  8. const int *const p = &test1;
  9. //p = &test2;
  10. //*p = 3;
  11. printf("%d\n", *p);
  12. return 0;
  13. }

执行结果 :1,这个就相当于以上两种情况的混合体,p是常量,所以不能把test2的地址赋给p;同时*p也是常量,所以*p的内容不可更改!

const int *p与int *const p的区别(转:csdn,suer0101)的更多相关文章

  1. const int * p 和 int const * p 和 int * const p 的区别

    首先注意,const int * p 和int const *p 是一样的,并且不管是不是*p,即使const int i和int const i也是一样的,所以我们接下来只讨论int const * ...

  2. C++ char*,const char*,string,int 的相互转换

    C++ char*,const char*,string,int 的相互转换   1. string转const char* string s ="abc";const char* ...

  3. const int *p 和int * const p 的区别

    看例子: int sloth = 3; const int *p1 = &sloth; int * p2 const = &sloth; 这样申明的话,不允许使用p1来修改sloth的 ...

  4. (c++) int 转 string,char*,const char*和string的相互转换

    一.int 和string的相互转换 1 int 转化为 string c++ //char *itoa( int value, char *string,int radix); // 原型说明: / ...

  5. error C2556: 'const char &MyString::operator [](int)' : overloaded function differs only by return type from 'char &MyString::operator [](int)'

    char & operator[](int i);const char & operator[](int i);/*const char & operator(int i);* ...

  6. [转] const int *a与int *const a,const int *const a的区别

    http://blog.csdn.net/zhangheng837964767/article/details/33783511 关键问题点:const 属于修饰符 ,关键是看const 修饰的位置在 ...

  7. const int *a与int *const a,const int *const a的区别

    来源:https://blog.csdn.net/zhangheng837964767/article/details/33783511 关键问题点:const 属于修饰符 ,关键是看const 修饰 ...

  8. could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'

    VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...

  9. const void *a 与 void *const a 的差别

    const void *a 这是定义了一个指针a,a能够指向随意类型的值,但它指向的值必须是常量. 在这样的情况下,我们不能改动被指向的对象,但能够使指针指向其它对象. 比如: const void ...

随机推荐

  1. Python脚本控制的WebDriver 常用操作 <七>浏览器前进和后退操作

    下面将使用WebDriver来控制浏览器的前进和后退操作 测试用例场景 此操作和get.url()方法功能相同 Python脚本 # coding=gbk ''' Created on 2013年12 ...

  2. Java当中的I/O的字节流

    I/O是input/output的缩写,即输入输出系统. I/O操作即从数据源中读取数据,以及将数据写入到数据目的地中.读的来源(如文件.键盘.网络)和写的目的地(如文件.屏幕.网络)分为很多种. 数 ...

  3. Xhprof安装笔记(PHP性能监控)

    由facebook开源出来的一个PHP性能监控工具,占用资源很少,甚至能够在生产环境中进行部署.它可以结合graphviz使用,能够以图片的形式很直观的展示代码执行耗时 wget http://pec ...

  4. hdu 1867 A + B for you again

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1867 A + B for you again Description Generally speaki ...

  5. iOS学习之UI可视化编程-XIB

    一.Interface Builder可视化编程 1.Interface Builder简介: GUI:图形用户界面(Graphical User Interface,简称GUI,又称图形用户接口)是 ...

  6. win10里安装.net3.5

    在CMD窗口里面输入这样一段,来安装Microsoft.MET Framework 3.5 Dism /online /enable-feature /featurename:NetFX3 /All ...

  7. AngularJS与RequireJS集成方案

    关于angularjs.requirejs的基础知识请自行学习 一.简单事例的项目目录如下: -index.html -scripts文件夹 --controller文件夹 --- mianContr ...

  8. [转]WinExec、ShellExecute和CreateProcess及返回值判断方式

    [转]WinExec.ShellExecute和CreateProcess及返回值判断方式 http://www.cnblogs.com/ziwuge/archive/2012/03/12/23924 ...

  9. Android实现AppWidget、Broadcast静态注册

    Android实现AppWidget.Broadcast静态注册 本篇博客是基于我上一篇博客继续修改的,详情请看Android实现AppWidget.Broadcast动态注册 开发工具:Andori ...

  10. Win8.1+vs2012+osg环境搭建

    Win8.1+vs2012+osg环境搭建 一.    相关准备 a) Osg源码 当前最新版:OpenSceneGraph-3.2.0.zip 下载链接: http://www.opensceneg ...