这是我在知乎回答的一个问题.
这个问题是C中的一个深坑,首先说结论:
char ** 和 const char ** 是两个不相容(incompatible)的类型,可以理解为不能直接赋值Each argument shall have a type such that its value may be assigned to an object with the unqualified version of the type of its corresponding parameter.翻译:每个实参都应该具有自己的类型,这样它的值就可以赋给对应非完全限定版本形参类型的对象
Constraints简单来说就是两个操作数都是指向有限定符或无限定符的相容的类型的指针,左边的指针必须有右边指针指向类型的全部限定符时赋值合法
1 One of the following shall hold:112)
(......省略......)
-- the left operand has atomic, qualified, or unqualified pointer type, and (considering
the type the left operand would have after lvalue conversion) both operands are
pointers to qualified or unqualified versions of compatible types, and the type pointed
to by the left has all the qualifiers of the type pointed to by the right;
(......省略......)
char *cp;
const char *ccp;
ccp = cp;
29 EXAMPLE 1 The type designated as ‘‘float *‘‘ has type ‘‘pointer to float‘‘. Its 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 -- its type is ‘‘pointer to const-qualified float‘‘ and is a pointer to a qualified type.这个例子是说const float * 类型并不是一个有限定符的类型,它是一个没有限定符的指针,指向的是有const限定符的float类型数据.也就是说const修饰的是指向的float而不是指针本身.
C语言中为什么不能把char**赋给const char**
原文地址:http://blog.csdn.net/shaw1994/article/details/39993509