Why can't I assign char** to the const char** in C language?

Source: Internet
Author: User

This is one of the questions I have answered.

This problem is a deep pit in C, first of all to say the conclusion:

char * * and const char * * are two incompatible (incompatible) types, which can be understood as not being directly assigned to a value
in C11 's 6.5.2.2 Function calls, there are the following
Each
argument shall has a type such that it value may assigned to an object with the unqualified version of the The type of its corresponding parameter.
translation: Each argument should have its own type so that its value can be assigned to an object corresponding to a non-fully qualified version parameter type
that is to say: The process of parameter passing and assignment is similar.
look at the constraints in 6.5.16.1 simple assignment
Constraints
1 One of the following shall hold:112)
(...... Omit ...)
--The left operand have atomic, qualified, or unqualified pointer type, and (considering
The type the left operand would has after lvalue conversion) both operands is
Pointers to qualified or unqualified versions of compatible types, and the type pointed
The left have all the qualifiers of the type pointed to by the right;
(...... Omit ...)
In a nutshell, two operands are pointers to compatible types that have qualifiers or no qualifiers, and the left pointer must have the right pointer pointing to all qualifiers of the type when the assignment is valid
It is because of this constraint that it is possible to assign an argument char* to a const char*, such as the following code for explanation:
char*cp;constchar*ccp;ccp=cp;
left operand CPP is a pointer to char with the Const qualifier
right operand CP is a pointer to char with no const qualifier
Char and char are compatible types, and the left pointer has a right pointer to all qualifiers of type (the type that the right pointer points to does not have a qualifier)
so this assignment is legal.
Note that the reverse is not.
6.5.16.1 Simple assignment Other constraints do not indicate that the char** assignment to the const char * * is legal. Most likely to prove that it is legal is the binding condition written above.
However, the "most likely to prove" constraint does not prove that the assignment is legal.
First Look at the example in 6.2.5 types
EXAMPLE 1 The type designated as ' float * ' has a type ' pointer to float '. It type category is pointer, not a floating type. The const-qualified version of this type is designated as ' float * const ' whereas the type designated as ' const float * ' is not a qualified type--it is "pointer to const-qualified float" and is a pointer to a qualified type.
This example is that the const float * Type is not a type with a qualifier, it is a pointer with no qualifier, and it points to the float type data with the Const qualifier. That is, the Const modifier refers to the float instead of the pointer itself.
Similarly, char * * and const char * * are pointers with no qualifiers.
Char * * points to a pointer with no qualifier modifier char *
Const char * * points to a pointer with no qualifier modifier const char *
but char * and const char * are incompatible, the constraints require the type to be pointed, regardless of whether there are qualifiers, but must be compatible, and obviously char * and const char * are two different pointers.
Although the type that char * and const char * points to is compatible, and you can assign the value of the previous pointer to the latter pointer, this does not mean that the two pointer types are compatible.
in summary, char * is incompatible with the const char *, which is inconsistent with the "most likely to prove" constraint, so there is no constraint that can justify assigning the value of char * * to the const char * *.
so the compiler displays that warning. Because these two pointers are not the same type at all.

Why can't I assign char** to the const char** in C language?

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.