2014-04-13 31 views

回答

5

之間「」初始化它如果你真的想初始化在聲明時你的結構對象,使用文字的化合物:

struct test_stub 
{ 
    int n; 
    int *array; 
    char *b; 
} 
test[3]= 
{ 
    {5,(int [5]){1,2,3,4,5},"abcd"} 
}; 

,或者如果你的陣列是固定大小的,的array類型從int *到改變10:

struct test_stub 
{ 
    int n; 
    int array[5]; 
    char *b; 
} 
test[3]= 
{ 
    {5,{1,2,3,4,5},"abcd"} 
}; 
+0

感謝您的迴應,但在使用此{5,(int [5]){1,2,3,4,5},「abcd」}時,我仍然在Visual Studio中出錯。我得到的錯誤是類型名稱是不允許的 – user3365619

+0

@ user3365619複合文字是c99功能,但Visual Studio不支持c99。 – ouah

+0

@ouah在第一種情況下,指針'array'可以傳遞給'free'嗎?爲它指向的數組分配的內存在哪裏? – ajay

相關問題