GCC编译错误:施放指定数组类型 [英] gcc compile error: cast specifies array type
问题描述
以下code是完美有效,
The following code is perfect valid,
int *ia = (int[]){1,3,5,7};
但是当我编译code的下一行,
but when I compile the next line of code,
char *p = (char[]) "abc";
GCC说
test.c:87: error: cast specifies array type
看来它们以相同的方式流延。没有第二个,为什么得到一个错误信息?
It seems they are casted in the same way. Why did the second one get an err msg?
正如你们所说,ABC是一个指针,它不能转换为一个指针。因此,我的另一个问题:为什么
As you guys said, "abc" is a pointer, which cannot be converted to be a pointer. So my another question: why does
char[] s = "abc";
是有效的。如何code的工作上面的行编译时?
is valid. How does the above line of code work when compiling?
推荐答案
这是有效的,因为在右侧的前pression是C99的复合文字的,不是施法:
This is valid because the expression on the right hand side is a C99 compound literal, not a cast:
int *ia = (int[]){1,3,5,7};
然而,这是因为它是一个的铸前pression 的,而不是字面化合物是无效的。由于GCC告诉你,你不能转换为数组类型:
However, this is not valid because it is a cast-expression, not a compound literal. As GCC is telling you, you can't cast to array types:
char *p = (char[]) "abc";
您可以通过使适当的复合文字修复它 - 他们是由括号表示:
You can fix it by making it a proper compound literal - they are denoted by the braces:
char *p = (char[]){"abc"};
这篇关于GCC编译错误:施放指定数组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!