Constants and pointers (
Pointers and constants
) -- Const-modified pointer confusing generally involves a pointer involved in a constant modified with const, which is easy to confuse with the head. A simple technique is to look from the right to the left, the following is an example: Const int * P1 = NULL ;// Method 1 Int const * P2 = NULL ;// Statement 2 Int * const P3 = NULL ;// If the value assignment statement is commented out and "// error" is added, the error code is displayed. Others are correct: P1 = NULL; P2 = NULL; // * P1 = 1; // Error // * P2 = 2; // Error // P3 = NULL; // Error * P3 = 0; It is equivalent to defining three pointers, P1, P2, P3, and writing one and writing two. Therefore, we mainly look at the differences between P2 and P3. Based on our right-to-left technique, it is to see the right side of const: P2: The right side of const is "* P2", and the defined constant is the constant pointed to by P2, then P2 = 0 is correct, * P2 = 0 error P2: the right side of const is "P3". If the defined constant is the pointer P3 itself, P3 = 0 is incorrect, * P3 = 0 is correct.