C Pointer compatibility issues

Source: Internet
Author: User

Pointer compatibility issues:

    1. A const pointer cannot be assigned a value to a non-const pointer.
    2. A non-const pointer can be assigned to a const pointer, but only if it is an indirect operation
1 Example:2     int*pt1;3         Const*pt2;4         Const**PT3;5     6PT2=PT1;//OK7Pt1=pt2;//NO8pt3=&pt2;//OK9pt3=&pt1;//NO Double indirectionTen problem: OnePt1=pt2;//NO Apt3=&pt2;//OK -pt3=&pt1;//NO Double indirection -      the Explain: -     Constn=5; -INT *P1; -Constint**P2; +     //consumption p2=&p1; is right -p2=&P1; +*p2=&n;//OK, but this mean p1=&n; A*p1=Ten;//OK because P1 isn't const, but that'll be contradicted with consumption

Pointer compatibility issues: Const pointers cannot be assigned to non-const pointers.      A non-const pointer can be assigned to a const pointer, but only if it is a layer of indirect operation 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 p  T3=&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 isn't const, but that'll be contradicted with consumption

C pointer compatibility issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.