2

可能重复:
零长度数组与指针

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

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);

某些新编译器强制执行此限制的任何原因?

编辑以纠正错误

4

2 回答 2

6

这有什么问题:

struct test t;

memcpy(string, test->data, 19);

? 提示,test是一个类型

编辑:至于真正的答案,请参阅这个问题:零长度数组与指针(或类似的问题)

于 2012-11-21T09:54:59.973 回答
0

数组不能有0size 。

这是标准:

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.
于 2012-11-21T10:01:58.820 回答