从unsigned char *到char *的无效转换 [英] Invalid conversion from unsigned char* to char*

查看:3664
本文介绍了从unsigned char *到char *的无效转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个代码 -

  1 int main(int argc, char *argv[])
  2 {
  3     signed char S, *psc;
  4     unsigned char U,  *pusc;
  5     char C, *pc;
  6 
  7     C = S;
  8     C = U;
  9 
 10     pc = psc;
 11     pc = pusc;
 12 
 13     return 0;
 14 }

$ gcc test.cpp -o a
test.cpp: In function ‘int main(int, char**)’:
test.cpp:10:7: error: invalid conversion from ‘signed char*’ to ‘char*’ [-fpermissive]
test.cpp:11:7: error: invalid conversion from ‘unsigned char*’ to ‘char*’ [-fpermissive]

这是编译在gcc版本4.6.3在Ubuntu 12.10在一个Intel 32

This is compiled on gcc version 4.6.3 on Ubuntu 12.10 on an Intel 32-bit machine.

考虑 char 类型是 unsigned char on x86。 -

如果第7行和第8行的非指针类型的赋值都是OK,为什么会抛出指针类型的错误在第10和11行?

If assignments on line 7 and 8 for non-pointer types are Ok, why errors are thrown for pointer types on lines 10 and 11 ?

此外, C = U

推荐答案

首先,需要强调的是, char signed char unsigned char 都是不同类型。 C ++ 11标准的第4.10节定义了不同类型的指针之间的三种可能的标准指针转换:

First of all, it is important to stress the fact that char, signed char, and unsigned char are all different types. Section 4.10 of the C++11 Standard defines the three possible standard pointer conversions between pointers of different types:


1。空指针常量是整数类型的整数常量表达式(5.19)prvalue,其值为零或std :: nullptr_t类型的prvalue。空指针常量可以转换为指针类型;结果是该类型的空指针值,并且可以与对象指针或函数指针类型的每个其他值区分开。这种转换称为空指针转换。相同类型的两个空指针值将比较相等。空指针常量到cv限定类型的指针的转换是单次转换,而不是指针转换的序列,后面跟着限定转换(4.4)。整数类型的空指针常量可以转换为std :: nullptr_t类型的prvalue。 [注意:生成的prvalue不是一个空指针值。 -end note]

1 . A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion. Two null pointer values of the same type shall compare equal. The conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the sequence of a pointer conversion followed by a qualification conversion (4.4). A null pointer constant of integral type can be converted to a prvalue of type std::nullptr_t. [ Note: The resulting prvalue is not a null pointer value. —end note ]

这是不相关的,因为我们没有类型的空指针 nulltptr_t 这里。

This is not relevant, since we don't have null pointers of type nulltptr_t here.


2。一个类型为指向cv T的指针的prvalue,其中T是一个对象类型,可以转换为类型pointer to cv void的prvalue。将指向cv T的指针转换为指向cv void的指针的结果指向类型T的对象所在的存储位置的开始,好像该对象是类型T的最大导出对象(1.8) (也就是说,不是一个基类子对象)。空指针值将转换为
目标类型的空指针值。

2 . A prvalue of type "pointer to cv T," where T is an object type, can be converted to a prvalue of type "pointer to cv void". The result of converting a "pointer to cv T" to a "pointer to cv void" points to the start of the storage location where the object of type T resides, as if the object is a most derived object (1.8) of type T (that is, not a base class subobject). The null pointer value is converted to the null pointer value of the destination type.

这不能应用,因为目标类型不是 void 。最后,

This cannot apply, since the destination type is not void. Finally,


3。类型指向cv D的指针的prvalue,其中D是类类型,可以被转换为类型指向cv B的指针的prvalue,其中B是D的基类(第10条)。如果B是不可访问(第11条)或模糊(10.2)D的基类,一个需要这种转换的程序是不成形的。转换的结果是指向派生类对象的基类子对象的指针。空指针值转换为目标类型的
空指针值。

3 . A prvalue of type "pointer to cv D", where D is a class type, can be converted to a prvalue of type "pointer to cv B", where B is a base class (Clause 10) of D. If B is an inaccessible (Clause 11) or ambiguous (10.2) base class of D, a program that necessitates this conversion is ill-formed. The result of the conversion is a pointer to the base class subobject of the derived class object. The null pointer value is converted to the null pointer value of the destination type.

signed char 不是 char 的基类,所以甚至不适用。

signed char is not a base class of char, so not even this applies.

因此,从 signed char char

Therefore, an implicit, standard pointer conversion from signed char to char cannot be performed.

另一方面,根据第4.7段中的规定,允许整数类型值之间的转换。

On the other hand, conversions between values of integral types are permitted according to what specified in Paragraph 4.7.

这篇关于从unsigned char *到char *的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆