本文只是一篇学习笔记,是看了《彻底搞定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. 小白学jquery Mobile《构建跨平台APP:jQuery Mobile移动应用实战》连载四(场景切换)

    作为一款真正有使用价值的应用,首先应该至少有两个页面,通过页面的切换来实现更多的交互.比如手机人人网,打开以后先是进入登录页面,登录后会有新鲜事,然后拉开左边的面板,能看到相册.悄悄话.应用之类的其他 ...

  2. Python可以做什么?

    Python是一个优秀的程序语言,PYthon的应用角色几乎是无限的,你可以在任何场合应用Python. 1 系统编程 Python提供对系统管理的内部接口,可以搜索文件和目录,可以运行其他程序 Py ...

  3. next permutaion算法

    算法描述: Find largest index i such that array[i − 1] < array[i]. Find largest index j such that j ≥ ...

  4. C/C++ 关于大小端模式

    大端模式:  数据的高字节存在低地址  数据的低字节存在高地址 小端模式:  数据的高字节存在高地址  数据的低字节存在低地址 如图,i为int类型占4个字节,但只有1个字节的值为1,另外3个字节值为 ...

  5. Android--启动拍照功能并返回结果

    因为没有深入学习拍照这块功能,所以只是简单的调用了一下系统的拍照功能,下面代码: //拍照的方法 private void openTakePhoto(){ /** * 在启动拍照之前最好先判断一下s ...

  6. 【分享】生成Revit扩展的addin文件小工具

    在进行Revit二次开发的时候,加载命令/程序使用的是添加addin文件的方式,每次都需要手动的写,而且参数有好多,很不方便.于是乎我有了写一个小工具的想法.进过研究终于完成了.主要使用RevitAd ...

  7. [转]ubuntu 10.04下的配置tftp服务器

    [转]ubuntu 10.04下的配置tftp服务器 http://www.cnblogs.com/geneil/archive/2011/11/24/2261653.html 第1步:安装tftp所 ...

  8. C#全局作用符::

    比如说你在全局定义了一个变量str,然后在函数里面又定义了这个str名字的变量的,这个时候你要是在函数里面直接写str,那么就是访问的函数内部的变量的.无法访问外部变量的.这是正常的现象的.但是如果你 ...

  9. Linux内核内存管理

    <Linux内核设计与实现>读书笔记(十二)- 内存管理   内核的内存使用不像用户空间那样随意,内核的内存出现错误时也只有靠自己来解决(用户空间的内存错误可以抛给内核来解决). 所有内核 ...

  10. pb datawindow 判断是否是最后一列最后一行

    li_column1 = GetColumn() ls_columnname = GetColumnName() Send(Handle(This),,,Long(,)) ll_row2 = GetR ...