关于const修饰指针的情况,一般分为如下4种情况:

int b =;
const int *a =&b; //情况1
int const *a =&b; //情况2
int* const a =&b; //情况3
const int* const a =&b; //情况4

如何区别呢?
  

如果const 位于星号的左侧,则const 就是用来修饰指针所指向的变量,即指针指向为常量;

如果const 位于星号的右侧,const就是修饰指针本身,即指针本身是常量。

1.情况1和2相同,都是指针所指向的内容为常量,这种情况下不允许对内容进行更改操作。

  例如

int b =;
const int *a =&b; //情况1
*a =; //错误

  但也有别的办法改变*a的值,一个是通过改变b的值,另一个是使a指向别处 a = &c 。

对于情况1和2,可以不进行初始化。因为虽然指针内容是常量,但指针本身不是常量。

2.情况3 为指针本身是常量,不能对指针本身进行更改操作,而指针所指向的内容不是常量。

  因此,定义时必须同时初始化。

3.情况4 为指针本身和指向的内容均为常量。

const修饰指针的更多相关文章

  1. C语言--const修饰指针解析

          这种例子在我们平时编程时可能会很少用,但是在面试时被问的可能性就很大了. 用const修饰指针,常见的情况可以分为四种: 1.const int *p; // p 可变,p 指向的对象不可 ...

  2. const修饰指针+volatile +restrict

    const这块的难点 const修饰指针有以下的几种形式 ,不同的形式 它的意义不一样. 形式1: int a=23: const int *p=&a: a是int型,&a是int * ...

  3. 关于const修饰指针

    const修饰指针,一般分为如下四种情况: int b = 500; const int *a = &b;   //情况1 int const *a = &b //        2 ...

  4. C++ --const修饰指针

    const修饰指针 1.const修饰指针 (常量指针)常量的指针 const int *p = &a; const修饰的是*p(表示内容为常量),不是p(指针) 指针指向的地址可以改,但指针 ...

  5. C++中使用const修饰指针

    在本文中呢,主要讲解四个方面,即:常量数据的与否和常量指针的与否中const如何对指针进行修饰: 1.指向非常量数据的常量指针 对于一个指向非常量数据的常量指针,我们应该清楚的知道,在这我们注重的是指 ...

  6. const修饰指针的三种效果

    当用const进行修饰时,根据const位置的不同有三种不同效果. 判断的标准是:const修饰谁,谁的内容就是不可变的. 1 const int *p = &a; const修饰*p, *p ...

  7. const修饰指针时应注意的问题

    1.指向常量的指针 顾名思义,一个指针指向const对象,则称它为指向常量的指针. ; int* ptr=&a; //错误 const int* ptr=&a; //必须用const来 ...

  8. C++中const修饰基本数据类型、指针、引用、对象

    const修饰基本数据类型 #include <iostream> using namespace std; void main(){ const int a = 1; const cha ...

  9. C++中的const和指针组合

    在C++里,const修饰指针有以下三种情况 (1)指针常量:即指向常量的指针 const  int *p或者int const *p const在*前,,可以这样理解它的功能,因为const在*前, ...

随机推荐

  1. CentOS安装solr 4.10.3

    Solr是什么? Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器.Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展,并对索 ...

  2. 2016 ACM/ICPC Asia Regional Qingdao Online HDU5878

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5878 解法:先保存,再二分查询~具体http://blog.csdn.net/coder_xia/arti ...

  3. Codeforces Round #260 (Div. 2) C

    Description Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. On ...

  4. C语言使用fread和fwrite处理任何文件

    1.文件必须以二进制形式打开 FILE* pfile1=fopen("fileone","rb"); FILE* pfile2=fopen("file ...

  5. 【leetcode❤python】 223. Rectangle Area

    #-*- coding: UTF-8 -*-#先判断是否有重叠class Solution(object):    def computeArea(self, A, B, C, D, E, F, G, ...

  6. 【leetcode❤python】 38. Count and Say

    #-*- coding: UTF-8 -*- class Solution(object):    def countAndSay(self, n):        """ ...

  7. Windows控制台程序“选定模式”的问题

    最近用Nodejs写了个代理程序,一直用的好好的,木有问题,今天突然发现不能用了,使用telnet去连代理的端口也能连通,可是服务就是不能正常使用,提示连接超时. 当时猜测是Nodejs的某个地方阻塞 ...

  8. Reading Csv Files with Text_io in Oracle D2k Forms

    Below is the example to read and import comma delimited csv file in oracle forms with D2k_Delimited_ ...

  9. Populate A List Item With Record Group In Oracle Forms Using Populate_List And Create_Group_From_Query Command

    Example is given below to Populate a List Item in Oracle Forms using Create_Group_From_Query , Popul ...

  10. 在服务器端如何提取checkbox提交的数据?

    HttpServeletRequest 单个字符串,getParameters() 多个字符串,getParametersValues(),返回一个数组,需要提前定义一个数组