c 指针兼容性问题

时间:2021-12-30 21:41:21

指针兼容性问题:   

  1. const指针不能赋值给非const指针.   
  2. 非const指针可以赋值给const 指针,但前提是只是一层间接运算   
   Example:
  int *pt1;
   const *pt2;
   const **pt3;
  
   pt2=pt1;//OK
   pt1=pt2;//NO
   pt3=&pt2;//OK
   pt3=&pt1;//NO double indirection
  Problem:
  pt1=pt2;//NO
  pt3=&pt2;//OK
  pt3=&pt1;//NO double indirection
  
  Explain:
  const n=;
  Int *p1;
  Const int **p2;
  //consumption p2=&p1; is right
  p2=&p1;
  *p2=&n;//OK ,but this mean p1=&n;
  *p1=;//OK because p1 is not const ,but that will be contradicted with consumption

指针兼容性问题:   const指针不能赋值给非const指针.   非const指针可以赋值给const 指针,但前提是只是一层间接运算   Example:   int *pt1;       const *pt2;       const **pt3;          pt2=pt1;//OK       pt1=pt2;//NO       pt3=&pt2;//OK       pt3=&pt1;//NO double indirection   Problem:   pt1=pt2;//NO   pt3=&pt2;//OK   pt3=&pt1;//NO double indirection      Explain:   const n=5;   Int *p1;   Const int **p2;   //consumption  p2=&p1; is right   p2=&p1;   *p2=&n;//OK ,but this mean p1=&n;   *p1=10;//OK because p1 is not const ,but that will be contradicted with consumption