简体   繁体   English

从unsigned char *到char *的转换无效

[英]Invalid conversion from unsigned char* to char*

Here is a code - 这是一个代码 -

  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]

This is compiled on gcc version 4.6.3 on Ubuntu 12.10 on an Intel 32-bit machine. 这是在英特尔32位机器上的Ubuntu 12.10上的gcc版本4.6.3上编译的。

Considering that char type is unsigned char on x86. 考虑到char类型是x86上的unsigned char - -

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 ? 如果非指针类型的第7行和第8行的赋值为Ok,为什么第10行和第11行的指针类型会抛出错误?

Also, should C = U succeeds without requiring a cast? 此外,如果C = U成功而不需要演员?

First of all, it is important to stress the fact that char , signed char , and unsigned char are all different types . 首先,重要的是强调charsigned charunsigned char都是不同的类型 Section 4.10 of the C++11 Standard defines the three possible standard pointer conversions between pointers of different types: C ++ 11 Standard的4.10节定义了不同类型的指针之间的三种可能的标准指针转换:

1 . 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. 空指针常量是整数类型的整数常量表达式(5.19)prvalue,其计算结果为零或类型为std :: nullptr_t的prvalue。 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). 将空指针常量转换为指向cv限定类型的指针是单个转换,而不是指针转换的序列,后跟限定转换(4.4)。 A null pointer constant of integral type can be converted to a prvalue of type std::nullptr_t. 可以将整数类型的空指针常量转换为std :: nullptr_t类型的prvalue。 [ Note: The resulting prvalue is not a null pointer value. [注意:生成的prvalue不是空指针值。 —end note ] - 尾注]

This is not relevant, since we don't have null pointers of type nulltptr_t here. 这是不相关的,因为我们这里没有nulltptr_t类型的空指针。

2 . 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”. 类型为“指向cv T的指针”的prvalue,其中T是对象类型,可以转换为类型为“指向cv void的指针”的prvalue。 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). 将“指向cv T的指针”转换为“指向cv void的指针”的结果指向T类型的对象所在的存储位置的开始,就好像该对象是类型T的最派生对象(1.8) (即,不是基类子对象)。 The null pointer value is converted to the null pointer value of the destination type. 空指针值将转换为目标类型的空指针值。

This cannot apply, since the destination type is not void . 这不适用,因为目标类型不是void Finally, 最后,

3 . 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. 类型为“指向cv D的指针”的prvalue,其中D是类类型,可以转换为类型为“指向cv B的指针”的prvalue,其中B是D的基类(子句10)。如果B是D不可访问(第11条)或模糊(10.2)基类,需要这种转换的程序是不正确的。 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 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. 因此,无法执行从signed charchar的隐式标准指针转换。

On the other hand, conversions between values of integral types are permitted according to what specified in Paragraph 4.7. 另一方面,根据第4.7段中的规定,允许在整数类型的值之间进行转换。

C ++没有自动指针转换,分配每一侧的指针类型都无关紧要,如果它们不同,则需要转换。

char is a distinct type from unsigned char and signed char . charunsigned charsigned char的不同类型。 It is only guaranteed to have equivalent value representation to one of them, but it is still a distinct type. 它只能保证对其中一个具有等价值表示,但它仍然是一种独特的类型。 You therefore cannot convert from either unsigned char* or signed char* to char* (that is, unless you use a reinterpret_cast ). 因此,您无法将unsigned char*signed char*char* (即,除非使用reinterpret_cast )。 C++ just doesn't allow pointer conversions between distinct types like this, because then one type could masquerade as another. C ++只是不允许像这样的不同类型之间的指针转换,因为那样一种类型可以伪装成另一种类型。

However, a conversion from either unsigned char or signed char to char is perfectly fine because it just involves a conversion of its value. 但是,从unsigned charsigned charchar的转换完全没问题,因为它只涉及其值的转换。

Consider it this way: you can convert an int to a float , but you can't convert an int* to a float* . 以这种方式考虑:你可以将int转换为float ,但是你不能将int*转换为float*

I could be wrong, but as said above, when you assigned "C = S; C = U;", C++ automatically converts it, kinda like if you do "char x = "h"; printf("%i", x);". 我可能是错的,但如上所述,当你指定“C = S; C = U;”时,C ++会自动转换它,有点像你做“char x =”h“; printf(”%i“,x );”。 However, pointers point to a specific location in memory, and that location has a size. 但是,指针指向内存中的特定位置,并且该位置具有大小。 So while converting sort of just looks at the values from a different angles, pointing to different values may involve changing the size of the value that is being pointed at. 因此,虽然转换类型只是从不同的角度查看值,但指向不同的值可能涉及更改指向的值的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM