2012-11-21 83 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