int *const && int const * && const int *的区别
ANSIC允许声明常量,常量和变量不同,常量就是不可以改变的量,用关键字const来修饰
比如:const int a
int const a
以上两种声明方式是一样的,我们不需要考虑const和int的先后顺序,按照你理解的方便的一中方式进行应用。
因为const和int的顺序先后并不影响结果,因此 int const * && const int *这两中情况就是一样的
所以我们只需要讨论两种情况
------------------------------------------1:int const *ptr
我们说过const int a;就是声明一个int型的常量,而int * ptr;是声明一个int型的指针变量,因此终上所述int const *ptr就是生命一个指向整形常量的指针,我们可以修改指针的值,但是不可以修改*ptr的值,也就是指针所指向的值;
#include<stdio.h>
int main(){
int a=;
int b=;
int const * p1=&a;
//(*p1)=12; 我们可以修改指针的值,但是不能修改他所指向的值
p1=&b;
printf("%d\n",*p1);
b=;
printf("%d\n",*p1);
return ;
}
上面的程序中,p1的值可被修改,可以指向另一个变量的地址,但是(*p1)的值不可以被修改了
首先const 修饰的是整个*pi(注意,我写的是*pi而不是pi)。所以*pi是常量,是不能被赋值的(虽然pi所指的b是变量,不是常量)。 其次,pi前并没有用const 修饰,所以pi是指针变量,能被赋值重新指向另一内存地址的。
------------------------------------------2:int * const ptr
#include<stdio.h>
int main(){
int a=;
int b=;
int * const p1=&a;
printf("%d\n",*p1);
(*p1)=;
//p1=&b; // 我们可以修改他所指向的值,但是不能修改指针的值
printf("%d\n",*p1); return ;
}
我们这里const修饰的p1,p1的值不能改变,但是我们可以改变他所指向的值
p1有了const的修饰,变成了一个指针常量,不能被修改
整个*p1没有被const修饰,所以*p1是变量,而不是常量
-----------------------------》3转换问题
#include<stdio.h>
int main(){ const int n=;
int *p;
p=(int *)&n;
printf("%d\n",*p);
*p=;
printf("%d\n",*p);
return ;
}
情况一:int * pi指针指向const int n常量的情况
const int n=10;
int *p;
p=&n;//这样可以吗?不行,VC下是编译错。const int 类型的n的地址是不能
//赋值给指向int 类型地址的指针p的。否则p岂不是能修改n的值了吗!
p=(int* ) &n; // 这样可以吗?强制类型转换可是C所支持的。
//VC下编译通过,但是仍不能通过*p=80来修改n的值。去试试吧!看看具体的怎样 情况二:const int * p指针指向const int n的情况
const int n=40;
const int * p;
p=&n;//两个类型相同,可以这样赋值。n的值无论是通过p还是n都不能修改的。 情况三:用const int * const p申明的指针
int n;
const int * const p=&n;
//你能想象p能够作什么操作吗?p值不能改,也不能通过修改n的值。因为不管是
//*p还是p都是const的
int *const && int const * && const int *的区别的更多相关文章
- [转] const int *a与int *const a,const int *const a的区别
http://blog.csdn.net/zhangheng837964767/article/details/33783511 关键问题点:const 属于修饰符 ,关键是看const 修饰的位置在 ...
- const int *a与int *const a,const int *const a的区别
来源:https://blog.csdn.net/zhangheng837964767/article/details/33783511 关键问题点:const 属于修饰符 ,关键是看const 修饰 ...
- C++ char*,const char*,string,int 的相互转换
C++ char*,const char*,string,int 的相互转换 1. string转const char* string s ="abc";const char* ...
- (c++) int 转 string,char*,const char*和string的相互转换
一.int 和string的相互转换 1 int 转化为 string c++ //char *itoa( int value, char *string,int radix); // 原型说明: / ...
- 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);* ...
- c#中const、static、readonly的区别
1. const与readonly const ,其修饰的字段只能在自身声明时初始化. Readonly 是只读变量,属于运行时变量,可以在类初始化的时候改变它的值.该类型的字段,可以在声明或构造函数 ...
- C# 总结const、 readonly、 static三者区别:
总结const. readonly. static三者区别: (有人问我,看似简单,我也没能立刻回答出来,总结一下,分享一下.) const:静态常量,也称编译时常量(compile-time con ...
- C++中加const与不加const的区别
“常量”与“只读变量”的区别. 常量肯定是只读的,例如5, "abc",等,肯定是只读的,因为常量是被编译器放在内存中的只读区域,当然也就不能够去修改它. “只读变量”则是在内存中 ...
- [转] const T、const T*、T *const、const T&、const T*& 的区别
这里的T指的是一种数据类型,可以是int.long.doule等基本数据类型,也可以是自己类型的类型class.单独的一个const你肯定知道指的是一个常量,但const与其他类型联合起来的众多变化, ...
- const T、const T*、T *const、const T&、const T*& 的区别
原文地址: http://blog.csdn.net/luoweifu/article/details/45600415 这里的T指的是一种数据类型,可以是int.long.doule等基本数据类型, ...
随机推荐
- 20145212 《Java程序设计》第6周学习总结
20145212 <Java程序设计>第6周学习总结 学习内容总结 第十章 1.Java将输入/输出抽象化为串流.读取数据对象成为输入流,能向其写入的对象叫输出流. 我从网上找到了一个可以 ...
- Kafka集群的安装和使用
Kafka是一种高吞吐量的分布式发布订阅的消息队列系统,原本开发自LinkedIn,用作LinkedIn的活动流(ActivityStream)和运营数据处理管道(Pipeline)的基础.现在它已被 ...
- delphi---控件使用
1.TBitBtn控件 属性:Glyph,指定要显示的位图: Layout ,设置位图在按钮的位置:Kind,要想用自设位图,这个属性要设置bkCustom; 2.TTreeView TTree ...
- jquery左边滚动,完毕后跳转回来
代码如下 function in_scroll(){ $liW = ; $num = $(".in-cy li").size(); $ulW = $liW*$num; $(&quo ...
- AOP PostSharp
using System; using System.Collections.Generic; using System.Linq; using System.Text; using PostShar ...
- URL中“#” “?” &“”号的作用
URL中"#" "?" &""号的作用 阅读目录 1. # 2. ? 3. & 回到顶部 1. # 10年9月,twit ...
- 年轻人,你活着不是为了观察K线做布朗运动
谈股票市场的赚钱陷阱 年轻人,你活着不是为了观察K线做布朗运动 作者:李晓鹏(2015-01-10) 这篇文章本来是该两年前写的,奉劝大家不要去玩股票.因为那个时候我的<中国崛起的经济学分析&g ...
- iOS- Could not find a storyboard named 'Main' in bundle NSBundle
1.删掉工程中main.storyboard 后要删除plist文件中对应的键值,否则会报如下错误: Could not find a storyboard named 'Main' in bundl ...
- android-android获取navigationview 上的控件id
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); View headerView = navi ...
- 【转】apache kafka监控系列-KafkaOffsetMonitor
apache kafka监控系列-KafkaOffsetMonitor 时间 2014-05-27 18:15:01 CSDN博客 原文 http://blog.csdn.net/lizhitao ...