【发布时间】:2012-12-20 15:31:31
【问题描述】:
可能重复:
C compile error: “Variable-sized object may not be initialized”
我遇到了一个问题,因为我的编译器仍然给我一个错误:可变大小的对象可能没有被初始化。我的代码有什么问题?
int x, y, n, i;
printf ("give me the width of the table \n");
scanf ("%d", &x);
printf ("give me the height of the table\n");
scanf ("%d", &y);
int plansza [x][y] = 0;
int plansza2 [x][y] = 0;
我当然想用“零”填充表格。
不幸的是,该程序仍然无法运行。这些表格的所有单元格上都显示有“416082”之类的数字。我的代码现在看起来像这样。:
int plansza [x][y];
memset(plansza, 0, sizeof plansza);
int plansza2 [x][y];
memset(plansza2, 0, sizeof plansza2);
printf("plansza: \n");
for(j=0;j<x;j++) {
for(l=0;l<y;l++) {
printf("%d",plansza[x][y]);
printf(" ");
}
printf("\n");
}
printf("plansza2: \n");
for(m=0;m<x;m++) {
for(o=0;o<y;o++) {
printf("%d",plansza2[x][y]);
printf(" ");
}
printf("\n");
}
【问题讨论】:
-
你和
memset()即将结识。