我聲明並嘗試初始化一個結構指針數組。它編譯沒有錯誤,但這個循環總是在8個迴路導致程序崩潰:初始化結構指針數組會導致奇怪的錯誤
for(ii = 0; ii < 10; ii++)
{
canArray[ii]->AC = 0;
printf("%d - AC is %d\n", ii, canArray[ii]->AC);
}
整個代碼在這裏:
typedef struct Can
{
int AC;
} Can;
int main (int argc, char* argv[])
{
int i, ii;
Can **canArray= malloc(10 * sizeof(Can[0]));
for (i =0; i < 10; i++)
{
canArray[i] = (Can*) malloc(sizeof(Can));
}
for(ii = 0; ii < 10; ii++)
{
canArray[ii]->AC = 0;
printf("%d - AC is %d\n", ii, canArray[ii]->AC);
}
}
你閱讀和理解的答案,你問前面的問題http://stackoverflow.com/questions/16313774/error-incompatible-types-when-initialising-struct- array/16313805#16313805? – 2013-05-01 09:21:00