首页 > 代码库 > c 指针兼容性问题

c 指针兼容性问题

指针兼容性问题:   

  1. const指针不能赋值给非const指针.   
  2. 非const指针可以赋值给const 指针,但前提是只是一层间接运算   
 1   Example: 2   int *pt1; 3       const *pt2; 4       const **pt3; 5    6       pt2=pt1;//OK 7       pt1=pt2;//NO 8       pt3=&pt2;//OK 9       pt3=&pt1;//NO double indirection10   Problem:11   pt1=pt2;//NO12   pt3=&pt2;//OK13   pt3=&pt1;//NO double indirection14   15   Explain:16   const n=5;17   Int *p1;18   Const int **p2;19   //consumption  p2=&p1; is right20   p2=&p1;21   *p2=&n;//OK ,but this mean p1=&n;22   *p1=10;//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

c 指针兼容性问题