Pointers and const 笔记和感受@备忘一篇
											
程序代码: #include<iostream>
using namespace std ;
/*什么是常量值的指针
指向常量值的指针是一个非const指针指向一个恒定值
常量值的指针的指向
1,指向常量值的指针指向一个恒定值,不可修改常量指针和常量值*/
void text7 ()
{  const int team =4;
  const int *p=& team;
  /*team =5;//此处为const int 指针 指向一个const int 型 的常量
  *p=6;因为指针是常量这种取消引用符号改变值的行为不可取
  cout<<*p;}*/可读不可改
//2.指向常量值的指针可以指向变量,会把变量值视为常量值,所以可以从常量中改变值,但是常量指针不可更改值
void text1 ()
{ int team =4;
  const int *p=& team;
  team =5;//从变量中改变值
  cout<<*p;
    }
void text2 ()
{   int team =4;
  const int *p=& team;
 // *p=5;//not okay,
// team=5;
  cout<<*p;
    }
//3.可以指向不同变量的值,因为指向const值的指针本身不是const(它仅指向const值),所以可以将其重定向为指向其他值:
 void text3 ()
{ int team =4;
  const int *p=& team;
    int teamp =5;
    p=&teamp;//此处指向了其他值
  cout<<*p;
    }
//常量指针,是一个恒定指针,一个常量指针  ,是一个指针,它的值初始化后不能更改
    void text4 ()
{ int team =4;
  int teamp =5;
   int *const p=& team;//常量指针是一个指针,它的值初始化后不能更改  
   // p=&teamp;//不可以
// cout<<*p;
    }
    void text5 ()
{ int team =4;
  int teamp =5;
  const int *const p=& team;//不能将指向const值的const指针设置为指向另一个地址,也不能通过指针更改它指向的值。
    
    //p=&teamp;这种是不可以
 // cout<<*p;
    }
       
int main ()
{ text1();//okay
  text3 ();
 text4 ();
    } 总结
可以将非常量指针重定向为指向其他地址。
const指针始终指向相同的地址,并且该地址不能更改。
指向非常量值的指针可以更改其指向的值。不可以更改常量的值
指向const值的指针会将值视为const(即使不是),因此无法更改其指向的值。 */
[此贴子已经被作者于2020-3-28 11:20编辑过]

											
	    