Compatibility of pointer assignment

Source: Internet
Author: User

This title is not accurate. Today, from expert C programming, it seems that the title has been moved to unknown places, and a bunch of things have been read in disorder. Let's start with expert C programming.

Today I read the beginning of this article, mainly in section 1.9.

Foo (const char ** p ){}

Main (INT argc, char ** argv)
{
Foo (arvg );
}

A warning is displayed: The parameter does not match the prototype. That is to say, char ** and const char ** are incompatible. However, char * and const char * are compatible. Why is char * compatible with const char * (char * P2 can assign a value to const char * P1), because the objects indicated by P1 and P2 are both char and compatible; p1 has const and P2 does not, which is also compatible because it complies with the rule that "the left value of the assignment between pointers must contain all the right-value qualifiers. But for const char ** P1 and char ** P2, the situation is different. Although P1 still complies with the rule that "the left value contains all qualified words of the right value", the object that P1 points to is a pointer to the const object, the object indicated by P2 is a pointer to a non-const object. The two types are incompatible, so p1 = P2 is invalid.

(From: http://blog.csdn.net/megaboy/archive/2005/10/17/505927.aspx)

In short, the const char * and char * pointer types are incompatible.

 

What is a compatible definition? If you see one on the internet, it makes sense: the type is compatible, which means the type is different, but the system can automatically convert it. That is to say, the system cannot automatically convert char * to const char *. In fact, I think this rule is quite strange. The char * and const char * pointers do not have any qualifier, but they only point to the object with a qualifier, one pointing to Char and the other pointing to const char.

That is to say, P1 is essentially a pointer Q1, and P2 also points to a pointer Q2. It is a pointer. Why is it incompatible. If the incompatibility is because Q1 and Q2 point to different objects, this is obviously a conflict. char * Q2 and const char * Q1 are obviously compatible. Therefore, this rule is simply strange. I don't know what the starting point is.

Obviously, there will be no perfect standards, and there will always be some minor problems.

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.