C language const keyword analysis from the perspective of read and write access

  1. Read and write permissions variables
    Here Insert Picture Description
  2. const: The variable is read-only
    Here Insert Picture Description
  3. const pointer modification:
    there are two cases, namely p * p and can be modified when const pointer, at the same time, the pointer can be changed by the value of the original variable to dereference the face of this situation, follow: rights can be the same or narrow pass, but not amplifying pass.
    Here Insert Picture Description
    The above code red wavy line represents the compiler is not passed, the focus of analysis: p1 = & b; // Error: Permission can not be magnified transfer, why not enlarge pass it?
    (1) because: if p1 = & b is true, then p1 can be referenced by solving indirect change the value of b, so that b is read but not written, both conflicts, look at p1 = & b, b is for, not himself write permissions, you can assign permissions p1? so can the same rights or reducing transmission, but can not pass amplification.
    (2) Similarly: p1 = ab; it is wrong. ab only read access, and p1 have read and write permissions, not natural.
    (3) const int * p2 and int const * p2 is the same type of data is transparent to the terms const.
    (4) const pointer to a variable or modified initial value, otherwise it does not make sense, const int * p2 quite special.

Guess you like

Origin blog.csdn.net/qq_42953408/article/details/86500757