Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中访问零长度数组地址时发生编译错误_C - Fatal编程技术网

在C中访问零长度数组地址时发生编译错误

在C中访问零长度数组地址时发生编译错误,c,C,可能重复: 一些新的编译器抛出以下情况的编译错误 struct test { int length; char data[0]; }; int main(void) { char string[20] = {0}; struct test *t; //Some code memcpy(string, t->data, 19); //Compilation error } 但是,如果我这样做,这个问题就会得到解决 memcpy(string, &(t->data[0

可能重复:

一些新的编译器抛出以下情况的编译错误

struct test {
int length;
char data[0];
};

int main(void)
{
char string[20] = {0};
struct test *t;

//Some code

memcpy(string, t->data, 19); //Compilation error 
}
但是,如果我这样做,这个问题就会得到解决

memcpy(string, &(t->data[0]), 19);
为什么一些新编译器会强制执行此限制


编辑以更正错误

这有什么问题:

struct test t;

memcpy(string, test->data, 19);
??提示,
test
是一种类型


编辑:关于真正的答案,请参见这个问题:(或类似的问题)

数组不能有
0
大小

标准如下:

ISO 9899:2011 6.7.6.2:

   If the expression is a constant expression, it shall have a value
   greater than zero
第二

使用以下命令:

memcpy(string,t->data,19); instead of what you have used.

请考虑如果你认为从编译器中得到实际的错误会有帮助,这是在ICC(英特尔C编译器)中发生的。这是错误:错误170:指针指向基础对象之外。对不起,它应该是MeMCPY(字符串,T->数据,19);是的,但这是在ISO c中,零长度数组的概念表示一个成员,后者可以是malloced。ISO C允许使用数组[];对于这个(注意没有给出大小),其中as gcc允许数组[0];