2013-10-18 137 views
9

我看到一段有效的C代码,我尝试将它编译为C++,并且出现了一个我无法理解的错误。从char *转换为signed char *

char* t; 
signed char* v = t; 

error: invalid conversion from char* to signed char*

从我的教训,charsigned char具有相同的语义,但仍被视为由编译器不同。

我知道这个错误是由这两种类型的差异引起的,我的问题是:为什么这种差异存在?

据我知道char被实现为一个或signed char作为unsigned char所以它应该是相同的一个或另一个。


我查阅了this question,它没有回答我想知道的观点。

+1

我喜欢把signed'的'和'无符号char'为*算术*类型,只是小的整数,本质上,而'char'是I/O类型 - 命令行参数,环境和通过文件读/写都是以字符的形式完成的。 –

+1

这个问题已经在http://stackoverflow.com/questions/436513/char-signed-char-char-unsigned-char回答 - 最基本的解释是'unsigned char'的范围从0到255, 'signed char'的范围是-127..128。所以你不能将-42的'signed char'转换为'unsigned char'或将''unsigned char''转换为'signed char'。 'char'通常读为'signed char'。 –

+0

@alle_meije和'signed char'和'char' *是相等的。我没有看到这个问题,虽然我搜索它... – Geoffroy

回答

9

其实我终于找到了规范部分谈到这一点:

3.9.1 Fundamental types

  1. Objects declared as characters (char) shall be large enough to store any member of the implementation’s basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single character literal form of that character. It is implementation-defined whether a char object can hold negative values. Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types. A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (3.11); that is, they have the same object representation. For character types, all bits of the object representation participate in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types. In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.
-1

我会说什么,我知道...

对于char类型C++有大小 '1' 字节..

如果是符号字符,则范围是从-128到127 否则,如果它是无符号的字符的范围是从0到256

大家都知道8位的情况下,一个字节对于符号字符,MSB(即,最左边的位)将用于符号,其余7位用于使值范围为0-2^7(0-127)。负号(逻辑1)和逻辑0 MSB的正面信号。例如(1 0000111 = -7,0 0000111 = + 7)和1 0000000-128。然而,如果为一个带符号的字符值赋值129,它将自动改变为-127(即范围内的值(-128,127)。

对于无符号字符类型的其他情况,所有8位都用于值,即范围是0-2^8(0-255),这里的0-127和signed char相同,属于-128到0的可以在128-255的unsigned char组中找到。我们可以说,并当场两种类型的“签名”和“无符号”,这可能是问题。

+0

@Geoffroy希望这有助于! – Raon

+0

感谢您的回答,这是真的,但它不是真正的问题:) – Geoffroy

+0

@Raon:实际上标准说:'sizeof(char)== 1',但不建立1 => 1字节,但只是'char'是系统支持的最小可分配内存块。一些深奥的系统可能会使用'1KB'作为尽可能小的可分配大小......还要注意标准没有指定签名类型的表示! – MFH

0

From what I learned, char and signed char are semantically identical, but are still >considered as different by the compiler.

NO之间的内部存储器的差异。char不是语义上相同到signed char

与其他整数类型(整数,长整数,短整数等)相比,不保证没有signedunsigned的char将为signed。这是实现定义的。一些架构在现实世界中把它定义为signed,其他如unsigned

所以,用一个char,如果符号性是很重要的,你真的需要指定你想要的。

我的建议是,如果你正在做字符操作等,或者使用使用charchar *的api调用,请使用char。如果你只是想要一个8位整数值,请确保你指定了signed charunsigned char,这样在几年之后,当你移植到不同的架构时,你不会被困在流氓中。

或更好的是,使用uint8_tint8_t 8位整数。

编辑:从您自己的答案:

These requirements do not hold for other types. In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.