Const modified pointer and quote

zhaozj2021-02-16  59

Const modified pointer and reference usage, people who begin school C are straightforward, I don't know how to be cloud. Once you know how to use it, everything is worth a bad. Below I have doubts for the reader: roughly tell them can be divided For three cases: Const modified pointer, const modified reference, const modified pointer references. 1.const modified pointer Const modifier pointer can be divided into three cases: const modified pointer itself constitutive variable (or object) Const modified pointer itself and pointer refers to the variable (or object) (1) .const modified pointer itself, the pointer itself is constant, can not change, any modified pointer itself is illegal. For example: const Int A = 1; const INT b = 2; int = 3; int J = 4; int * const pi = & i; // OK, PI type is int * const, & i type is int * Const Int * Const Pi = & a; // error, the type of PI is int * const, & a type of const * const pi = & j; // error, pointer is constant, not variable * pi = a; // ok, * pi does not Limited is constant, which can thus be seen that PI is constant, and the type of constant is strictly consistent when the constant is initialized and assigned. That is, when the const modifier is itself, the type of variables on both sides must be strict, otherwise it cannot be matched. (2) .const modified pointer points to the variable (or object), in this case, the value of the variable cannot be changed by the indirect reference pointer, assuming that the pointer is P, then * p is not variable, the following explanation: const INT * PI = & a; // or int const * pi = & a; // The two are no two, but BS likes the former, this is not technically the advantages and disadvantages, that is, Const Int and int Const can be interchangeable. It is recommended to be familiar / Take these two forms, for simple, and then use the former. // ok, const and do not modify the pointer itself, PI is not required to assign value //, but PI is an int * type pointer, so the must be a must Address value. Const int * pi = & i; // OK, PI can assign a constant address, but also be equivalent to address const * pi1 = & a; const INT * pi = pi1; // ok * pi = j; // error, * pi is not variable, can not change the indirect reference form of the pointer PI = & J; // OK, Pi variable PI = & b; // ok, pi variable PI ; // ok --pi; // ok is seen, PI is a variable, which can assate a value of constant and variables. As a integer variable can be assigning the integer and integer variables. Constly modified is not a pointer itself, but an indirect reference, = number of two sides do not have strict match, Such as: const * pi = & a; the type of PI is int *, and the type of & a is const INT * Const, as long as it contains INT *. Another example: const INT * PI = & J; medium, the type of PI is int *, and the type of & j is int * const, it is not a case to PI.

(3) Const modified pointer itself and pointer refers to the variables (or object) of the pointer (or object), which is pointed, P and * P are not variable. For example: const * const pi = & a; // or int Const * const pi = & a; // While Const Pi, it is the same as (2), but the PI must be constant, as mentioned above, the type of both sides does not have strict match, but must contain Int *, the type of & a is const INT * const, containing int *, so you can assign a value. Const int * const pi = & i; // ok, & i type INT * const, INT *, can be assigned. Const int * pi1 = & j; const * const pi = pi1; // ok, PI1 type is int * pi = & b; // error, pi non-variable pi = & j; // error, pi non-variable * pi = B // error, * pi * pi = j; // error, * pi non-variable PI ; // error, PI non-variable i; // OK, = 号 Variable (or object) and The modified variable has nothing to do with A-; // Error, A is constant, contacting the above two situations. For Const Int * const pi = & a; we can see: const INT * (const pi) = & a; (only expressions need), regard Const Pi as an integration, in line with the above classification (2). As long as INT * can be contained. 2. Const modification This situation is relatively simple, and it is not as comparable to the modifier pointer, because reference and reference objects are integrated, so reference is only modified by const modification. Const modification, reference itself is not variable, but reference variables (or objects) can be changed. For example: const Int & ri = a; // or int const & ri = a; ok, ri itself is constant, reference non-distinguished CONST INT & RI = I; //, reference is not distinguished by Type Ri ; // error, ri is constant, non-variable I ; // OK, = Right variable and reference independent RI = B; // error, ri is constant i = j; // ok, = Int & Const Ri = I; // Error, there is no meaning in this form, no meaning

转载请注明原文地址:https://www.9cbs.com/read-21853.html

New Post(0)