本文只是一篇学习笔记,是看了《彻底搞定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. PIL不能关闭文件的解决方案

    今天写了一个能指定图片尺寸,以及比例 来搜索分类图片的Python脚本.为了读取多个格式的文件的头,采用了Python PIL库. im = PIL.Image.open(imPath) if im的 ...

  2. CSV 文件读取类

    class CsvReader { private $csv_file; private $spl_object = null; private $error; public function __c ...

  3. 一篇文章教你读懂Makefile

    makefile很重要      什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professiona ...

  4. python 实现求和、计数、最大最小值、平均值、中位数、标准偏差、百分比。

    import sys class Stats: def __init__(self, sequence): # sequence of numbers we will process # conver ...

  5. 在windows上使用symfony创建简易的CMS系统(一)

    http://blog.csdn.net/kunshan_shenbin/article/details/7164675 参考自:http://xsymfony.801.cxne.net/forum. ...

  6. Linux下如何使CP命令不提示覆盖

    在Linux下使用CP命令,经常会提示是否覆盖,如果是太批量的文件覆盖,老是这么提示,会很烦的.那如何解决这个问题呢? 我们先来看一下原因吧! 一般我们使用的命令是cp -rf sourcefile ...

  7. [转]基于AngularJS的前端架构(上)

    模块化 怎么分模块 AngularJS自己有模块的概念,但只是为controller.direcitive.service等提供一个集合的概念,并没有文件调度的功能. 官方推荐的模块分类方法是: an ...

  8. 华硕电脑安装ubuntu出现问题及决方案

    问 题 一:华硕电脑安装ubuntu时无线网络禁用解决方案:打开终端(Ctrl+alt+t)运行命令sudo rmmod acer-wmi,然后开启无线,连接上后便可以上网(附上ubuntu论坛上讨论 ...

  9. 修改ip脚本

    1.打开运行 2.输入CMD 3.在命令提示符下输入: netsh -c interface ip dump > C:\我的网络配置.txt 4.打开您在C:\ 下的"我的网络配置 . ...

  10. AppDelegate中的方法解析

    // 当应用程序启动完毕的时候就会调用(系统自动调用) -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOp ...