指针、指针常量与常量指针的理解
2018-01-19 14:13
218 查看
#include<iostream> using namespace std; int main(){ int i = 1; const int j = 2; const int k = 3; int l = 4; /*========================普通指针,指针常量,常量指针========================*/ int *p = &i;//普通指针 cout << "the value of *p is: "<<*p<<"\n"<<endl;//输出为1 i = 10; cout << "the value of *p is: "<<*p<<"\n"<<endl;//输出为10 p = &l; cout << "now the value of *p is: "<<*p<<"\n"<<endl;//输出为4 const int *q = &j;//指向常量的指针,即指针常量 cout<<"the value of *q is: "<<*q<<"\n"<<endl;//输出为2 //*q = 10; wrong //j = 20; wrong,j为常量 //int *y = &j;//wrong,要存放常量对象的地址,只能使用指向常量的指针q,不能使用普通指针y q = &l;//指向常量的指针可以指向不是常量的对象,但不能通过*q改变l的值 //(因为指向常量的指针本身是用来指向常量的,使用*q不能改变常量的值, //即使q指向普通对象也是不能用*q去改变普通对象的值) cout<<"now the value of *q is: "<<*q<<"\n"<<endl;//输出为4 l=50;//right,但可以直接改变对象的值 cout<<"now the value of *q is: "<<*q<<"\n"<<endl;//输出为50 int *const n = &i;//n常量指针,指向的地址不能变化,但指向的地址的值可以变 //(n存放的地址为常量,不能变,但存放地址的值可以变) cout<<"the value of *n is: "<<*n<<"\n"<<endl;//输出为10 //n = &l;wrong,指向的地址变化了 i=40;//right,n指向i的地址的值可以变 cout<<"the value of *n is: "<<*n<<"\n"<<endl;//输出为40 *n = 100;//right,根据其所指的是常量或非常量而定,如果n所指的是常量,则*n不能变 cout<<"the value of *n is: "<<*n<<" and i is: "<<i<<"\n"<<endl;//输出都为100 return 0; }
相关文章推荐
- 深入理解C指针学习笔记之常量和指针(六)
- C++中常量指针,指针常量(const 和*)的使用方法和理解方法
- 关于指针常量和常量指针的理解
- C++中对常量指针和 指向常量的指针等组合声明的理解
- 指针常量与常量指针的理解
- c++高级---C++类中的常量以及cosnt指针的理解
- 指针常量C语言关键字const再理解
- C++初学之 5.常量指针和指针常量的理解
- C++ 语法实验室之指针、常量const、字符串和等号初学误区理解
- 一点一滴:理解常量指针
- const修饰符,以及指针常量,常量指针的理解
- C++中指针常量和常量指针的理解
- c++高级---C++类中的常量以及cosnt指针的理解
- 关于C语言中常量指针和指针常量的一点理解
- 一个例子程序对常量指针的理解
- 关于常量指针 和 指针常量及 typedef 中一些难以理解的知识
- 根据一段代码理清一些变量,常量,数组,指针的关系及理解
- 理解双指针
- 结构体中的链表指针以及->的含义,内存角度理解最易懂
- 关于指针的一些理解