我声明并尝试初始化一个结构指针数组。它编译没有错误,但这个循环总是在 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);
}
}