Learn more about const pointer

Source: Internet
Author: User

A problem occurs in the group today. The problem is as follows: the first assignment is acceptable. Why is the second assignment unavailable? [cpp] int * x = NULL; int const * y = NULL; y = x; int ** z = 0; int const ** n = 0; n = z at the time I paused, and then I thought it might be an int const ** problem, change the code to [cpp] int ** z = 0; int * const * n = 0; n = z; after compilation, I thought the problem was solved and I didn't think about it, and send it back to the questioner. Unexpectedly, after the questioner asked several questions, I asked again ,-_-! Which side does this const belong? It can be [cpp] int (* const) * n = 0; then n is an int pointer to (int * const) or [cpp] int * (const *) n = 0; then n is a const int pointer pointing to (int *). Then the questioner sends the following sentence: If const and (or) the volatile keyword is followed by the type specifiers (such as int and long). It acts on the type specifiers. In other cases, const and (or) the volatile keyword acts on the pointer asterisk next to its left. I think this sentence is not accurate enough. Generally, we will write the code as [cpp] const int * p. This is consistent with the description in the book. In this case, const acts on int. However, we also have another way to write [cpp] int const * p. Although we all know that this const must act on int, but according to the method in that sentence, const cannot find the target location. Okay, this is a bit of a horn. Since it is from the book, we still need to use code to prove it. The regression from the experiment to the top is actually the type matching problem. If the type does not match, the compilation will report an error. Therefore, we use the compiler (VS2010) as our referee, who is the role of const. First, set two required variables: [cpp] int * p_to_v_1 = new int (1); int * p_to_v_2 = new int (2 ); in the first experiment, [cpp] int ** ppTest1 = & p_to_v_1; ppTest1 = & p_to_v_2; * ppTest1 = p_to_v_2; ** ppTest1 = 10; this should not be a big problem, so do not explain. No error is reported after compilation. Conclusion: * ppTest1 is of the int type. ** PpTest1 is of the int type. Experiment 2 [cpp] int const ** ppTest2 = & p_to_v_1; ppTest2 = & p_to_v_2; * ppTest2 = p_to_v_2; ** ppTest2 = 10; this is relatively simple, ppTest2 points to the int const * pointer, so the third ** ppTest2 value assignment compiler reports an error: The expression must be a modifiable left value. * PpTest2 is also of the int const * (or const int *) type. ** PpTest2 is of the const int type and cannot be changed. Therefore, an error occurs when assigning values. Experiment 3 [cpp] int * const * ppTest3 = & p_to_v_1; ppTest3 = & p_to_v_2; * ppTest3 = p_to_v_2; ** ppTest3 = 10; const comes to the center of two asterisks, that's the problem I encountered above. The compiler returns the * ppTest3 = p_to_v2; this line of code. The error is: The expression must be a modifiable left value. P_to_v2 is the pointer variable. * ppTest3 is the pointer constant * ppTest3 is the int * const type. (Confirmed the sentence in the book) ** no error is reported in the ppTest3 line. ** ppTest3 is the fourth test of the int type [cpp] int ** const ppTest4 = & p_to_v_1; ppTest4 = & p_to_v_2; * ppTest4 = p_to_v_2; ** ppTest4 = 10; the error message is "ppTest4 = p_to_v2;". The error message is the same as in the preceding example, ppTest4 is a pointer constant pointing to the pointer. ppTest4 cannot be changed. Fifth test [cpp] int const * ppTest5 = & p_to_v_1; ppTest5 = & p_to_v_2; * ppTest5 = p_to_v_2; ** ppTest5 = 10; with the previous Foundation, it is easy to determine which error is reported here. Yes, row 3rd and row 4th are incorrect. * ppTest5 is a pointer constant, and ** ppTest5 is a constant. Experiment 6 [cpp] int * const * ppTest6 = & p_to_v_1; ppTest6 = & p_to_v_2; * ppTest6 = p_to_v_2; ** ppTest6 = 10; this definition looks a bit strange, although there will be a compilation warning, but it can pass. Only 3rd rows (* ppTest6 = p_to_v_2;) are reported because * ppTest6 is a pointer constant. Why is no error reported for Row 3? PpTest6 is a pointer to a pointer constant, and the result of & p_to_v_2 is a pointer to a pointer variable. Because the assignment principle is the same as that of the const int * type which can be assigned a value by the int * type (for example, the parameter in the void f (const int * cp) function. When f is called, the input parameter can be a const int * or an int *), a pointer to an int constant, and a pointer to an int variable. Here, the definition (int (* const) (const *) ppTest6) also needs to be noted. As mentioned earlier, ppTest6 is a pointer to a pointer constant, * ppTest6 is a pointer constant, which is equivalent to [cpp] int * const * ppTest6 7 test [cpp] int * const ppTest7 = & p_to_v_1; ppTest7 = & p_to_v_2; * ppTest7 = p_to_v_2; ** ppTest7 = 10; errors are reported for rows 2 and 3. ppTest7 is a constant, and * ppTest7 is also. Experiment 8 [cpp] int const * const ppTest8 = & p_to_v_1; ppTest8 = & p_to_v_2; * ppTest8 = p_to_v_2; ** ppTest8 = 10; errors are reported in steps 2, 3, and 4. You don't have to explain them... To sum up, most of the statements just now are correct, which is worse than the one I mentioned just now. If the const and (or) volatile keywords follow the type specifiers (such as int and long), it acts on the type specifiers. In other cases, const and (or) the volatile keyword acts on the pointer asterisk next to its left. The solution just described the different roles of const in different positions. Now let's look back at the original problem: [cpp] int * x = NULL; int const * y = NULL; y = x; int ** z = 0; int const ** n = 0; n = z. Now we can see these pointers: z is the pointer to int; n is the pointer to the const int *. n and z finally point to the type (one is int and the other is const int), so they cannot be assigned. As shown in the following code [cpp] int * x = NULL; int * const y = NULL; y = x;

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.