为什么可以使用字符串文字来初始化unsigned char数组但不能初始化unsigned char指针?

jin*_*wee 6 c type-conversion

我尝试使用gcc -Wall -pedantic-errors -std=c89以下代码进行编译:

int main(){
  unsigned char a[] = "foo";
  unsigned char *b= "foo";
  unsigned char *c= ( unsigned char *) "foo";
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么第二次初始化会引发错误pointer targets in initialization differ in signedness,但允许其他两个声明?

看来,在第二种情况下,从隐式转换char *unsigned char *不这样做.

PSk*_*cik 5

从技术上讲,因为标准明确允许使用字符串文字初始化(任何)字符类型的数组(6.7.9p14):

字符类型数组可以由字符串文字或 UTF-8 字符串文字初始化,可以选择用大括号括起来。字符串文字的连续字节(如果有空间或数组大小未知,则包括终止空字符)初始化数组的元素。

而对于大多数指针转换,标准需要显式转换(6.5.4p3):

涉及指针的转换,除非6.5.16.1的约束允许,否则应通过显式强制转换来指定。

直观地说,因为你可以这样做:

unsigned char a0 = 'f', a1 = 'o', a2 = 'o';
Run Code Online (Sandbox Code Playgroud)

或者换句话说,因为您可以使用不同的整数类型初始化整数类型,而无需显式转换。