2012-11-21 82 views
2
访问零长度数组的地址

Possible Duplicate:
zero length arrays vs. pointers编译错误,而用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]), 19); 

为什么一些新的编译器正在执行此限制的任何原因?

编辑,以纠正错误

+3

,如果你认为它会请考虑有助于包含您从编译器逐字逐句获得的实际错误。 – unwind

+0

你在x86上使用gcc吗? – pedr0

+0

这种情况发生在icc(intel c编译器) – nkumar

回答

6

有什么不对的:

struct test t; 

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

?提示,test类型

编辑:作为真正的答案,看到了这个问题:(上SO或类似的问题)zero length arrays vs. pointers

+0

对不起,应该是memcpy(string,t-> data,19); – nkumar

0

阵列不能有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. 
+0

是的,但是这是在ISO c中,零长度数组的概念表明一个后者可以被配对的成员是常见的。 ISO C允许使用array [];为此(注意没有给出的大小)在哪里作为gcc允许数​​组[0]; – fayyazkl