Const and pointer in C language-Why cannot I assign a value to const char Using char **?

Source: Internet
Author: User

 

In C, the const qualifier specifies an unchangeable variable, but it is not a constant. It can enable the compiler to help us ensure that some variables are not accidentally modified, such as when a pointer or reference is passed, adding const can ensure that our source data is not damaged. Const is much more complicated to modify pointers than in general cases. Let's take a look at the following example:

1 const char * p1; // The content pointed to by p1 is unchangeable

2 char const * p2; // The content pointed to by p2 cannot be changed

3 char * const p3; // p3 variable, but the content to which it points is variable

It can be seen from the annotation that when const is before *, it modifies the content pointed to by the pointer, but the pointer itself is variable, we can use it to traverse a source array or other groups of data that do not want to be accidentally broken. When const is behind *, it modifies the pointer itself, however, the content it points to is variable. In this sense, it is similar to the array name, but the two are not completely equivalent.

Under any circumstances, you can pass in a T-type pointer to a const T-type pointer (T indicates any data type), but in turn cannot. The reason is very simple. If it is feasible, we can use a normal pointer to modify the content pointed to by the const pointer. This is probably not the result we want. But does this rule apply to the level-2 Pointer Modified by const? For example, is it safe to pass a char ** value to the const char ** pointer? In this case, the compiler generally provides a warning. I thought this warning was irrelevant, but the compiler was too sensitive. Only later I thought that this example would help me understand what it was:

1 const char c = 'X ';

2 char * p1;

3 const char ** p2 = p1; // assign the value of char ** to const char **

4 * p2 = & c; // use const char * to assign a value to const char *. No problem at all.

5 * p1 = 'y'; // assign a value to the content pointed to by char *. No problem! But what will happen here?

Except that there is a warning in row 3rd, there is no problem with the rest, but after step 1, p1 has pointed to c, which is an unchangeable variable, step 2 !!! God, what have I done ??

This article is from the blog "xuexiangjing ".

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.