简体   繁体   English

为什么C ++不为字符数组接受有符号或无符号字符

[英]Why doesn't C++ accept signed or unsigned char for arrays of characters

I am using C++ in native mode with Visual Studio 2017. That compiler compiles the statement below without complaint: 我在Visual Studio 2017中以纯模式使用C ++。该编译器编译下面的语句而没有抱怨:

const char * AnArrayOfStrings[]  = {"z1y2x3w4", "Aname"};

However, if I change the above statement to specify that char is signed or unsigned, the compiler emits a C2440 error. 但是,如果我更改上述语句以指定char是有符号或无符号的,则编译器会发出C2440错误。 For instance, the statements below, do not compile: 例如,下面的语句编译:

const signed   char * AnArrayOfStrings2[] = {"z1y2x3w4", "Aname"};

const unsigned char * AnArrayOfStrings2[] = {"z1y2x3w4", "Aname"};

I fail to see the reason for the compiler refusing to compile the statement when the sign of char is made explicit. 我没有看到编译器在明确表示char的符号时拒绝编译语句的原因。

My question is: is there a good reason that I have failed to see for the compiler refusing to compile those statements ? 我的问题是:有没有一个很好的理由我没有看到编译器拒绝编译这些语句?

Thank you for your help (I did research in StackOverflow, the C++ documentation, I used Google and, consulted about a dozen C/C++ books in an effort to find the answer myself but, a reason still eludes me.) 感谢您的帮助(我在StackOverflow中进行了研究,C ++文档,我使用了Google,并且为了自己找到答案而咨询了大量的C / C ++书籍,但仍有一个原因让我望而却步。)

"z1y2x3w4" is const char[9] and there is no implicit conversion from const char* to const signed char* . "z1y2x3w4"const char[9]并且没有从const char*const signed char*隐式转换。

You could use reinterpret_cast 你可以使用reinterpret_cast

const signed char * AnArrayOfStrings[]  = {reinterpret_cast<const signed char *>("z1y2x3w4"),
                                           reinterpret_cast<const signed char *>("Aname")};

If you compile the above code 如果您编译上面的代码

const signed   char * AnArrayOfStrings2[] = {"z1y2x3w4", "Aname"};  

in C with gcc using options -Wall then it will give the following warning 在C中使用gcc选项-Wall然后它会发出以下警告

test.c:5:49: warning: pointer targets in initialization differ in signedness [-Wpointer-sign]
  const unsigned   char * AnArrayOfStrings2[] = {"z1y2x3w4", "Aname"};
                                                 ^
test.c:5:49: note: (near initialization for 'AnArrayOfStrings2[0]')
test.c:5:61: warning: pointer targets in initialization differ in signedness [-Wpointer-sign]
  const unsigned   char * AnArrayOfStrings2[] = {"z1y2x3w4", "Aname"};  

The type of elements of AnArrayOfStrings2 and "z1y2x3w4" are different. AnArrayOfStrings2"z1y2x3w4"的元素类型不同。 AnArrayOfStrings2[0] is of type const signed char * while "z1y2x3w4" is of type const char[9] . AnArrayOfStrings2[0]的类型为const signed char *"z1y2x3w4"的类型为const char[9]
The same code will raise error in C++. 相同的代码会引发C ++中的错误。 You will need explicit cast to make it work in C++. 您需要使用显式强制转换才能使其在C ++中工作。


To explain why 解释原因

const char * AnArrayOfStrings[]  = {"z1y2x3w4", "Aname"}; 

works I will take s simple example 我会举一个简单的例子

const char c[] = "asc";
const char *p1 = c;           // OK
signed const char *p2 = c;    // Error
unsigned const char *p3 = c;  // Error

In the second line of the above snippet, c will convert to const char * thus making p1 and c compatible types. 在上面代码片段的第二行中, c将转换为const char *从而使p1c兼容类型。
In third line the type of p2 and c are incompatible and compiler will raise an error in C++ (a warning in C). 在第三行中, p2c的类型是不兼容的,编译器将在C ++中引发错误(C中的警告)。 Same will happen with line 4. 第4行也会发生同样的情况。

If we take another example for int type 如果我们采用另一个int类型的例子

const int i[] = {1,2,3};
const int *ii = i            // OK
signed const int *si = i;    // OK
unsigned const int *usi = i; // Error  

First two pointer initializations work as int without any specifier is equivalent to signed int (but this is not true with char ) and therefore types are compatible. 前两个指针初始化作为int工作,没有任何说明符等同于signed int (但这不适用于char ),因此类型是兼容的。 Intialization fails in last case as const int * or signed const int * is incompatible with unsigned const int * . 在最后一种情况下,初始化失败,因为const int *signed const int *unsigned const int *不兼容。

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

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