Do you know: Char ** P and const char ** P are incompatible

Source: Internet
Author: User

Char ** P and const char ** P are two incompatible types, because they are two different types. If you understand this sentence, you don't have to read it.

 

First look at thisCode:

1 Foo ( Const   Char   ** P ){}
2
3 Void Main ( Int Argc, Char   ** Argv)
4 {
5 Foo (arvg );
6 }

 

If this code is compiled, the compiler sends a warning message:

Line 5: Warning argument is incompatible with prototype

(Row 3: warning: the parameter does not match the prototype ).

 

The question is: the real parameter char * s is compatible with the form parameter const char * P (all string processing functions in the standard library look like this ), why is the real parameter char ** argv incompatible with the form parameter const char ** P?

 

The answer is that they are incompatible.

In the ansi c standard, each real parameter should have its own type, in this way, the value can be assigned to the object of the corresponding parameter type (the object type cannot contain a qualifier ).

 

This means that the parameter passing process is similar to assigning values. Therefore, unless a char ** type value can be assigned to a const char ** type object, a Diagnostic message is generated. To make the assignment legal, you must meet one of the following conditions:

Both operands are pointer to compatible types with or without delimiters. The type pointed to by the Left pointer must have all the delimiters of the type pointed to by the right pointer.

This condition allows the real parameter char * in the function call to match the const char.

1 Char   * CP;
2 Const   Char   * CCP;
3 CCP = CP; // Right
4 CP = CCP; // Generate compilation warning

 

 

To thoroughly understand why char ** is incompatible with const char **, let's first review const float *: it is not a type with a qualifier -- its type is "pointer to a float type with a const qualifier", that is, the const qualifier is the type pointed to by the modifier pointer, instead of the pointer itself.

Similarly, const char ** is also a pointer type without a qualifier. Its type is "pointer to a char type pointer with a const qualifier ".

Because char ** and const char ** are pointer types without delimiters, But they point to different types (the former points to char * and the latter points to const char *), therefore, they are two different types and are incompatible. Therefore, parameters with the actual participation type of char ** being const char ** are incompatible. It violates the constraints listed in the italics listed above.

 

Note: Compile the first program in VC and.Program0 warnings and 0 errors. If GCC is used for compiling in Linux, the system will provide the warning of incompatible real parameters and form parameters. Does this mean that Microsoft's compiler is not good?

 

The question comes from expert C programming. Thank you for your criticism.

 

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.