我想知道c中的数组是如何工作的。所以我正在实现一些基本的数组概念。当我运行程序时,我得到了确切的输出,但在输出结束时它说分段错误。打印输出时出现分段错误
int main(void)
{
int a[] = {};
printf("Enter the number:");
int n = get_int();
int m = 0;
for(int i = 0; i<n; i++)
{
printf("insert:");
m = get_int();
a[i] = m;
}
for(int j = 0; j < n; j++)
{
printf("%d\n", a[j]);
}
}
输出:
Enter the number:3
insert:1
insert:2
insert:3
1
2
3
~/workspace/ $ ./arr_test
Enter the number:5
insert:1
insert:2
insert:3
insert:4
insert:5
1
2
3
4
5
Segmentation fault
看到它的尺寸为3它并不显示segmentation fault
第一输出但对于第二个具有的尺寸为5它显示。所以为什么会发生这种情况,我犯了什么错误
int a [] = {};'是标准C中的错误。我建议在标准兼容模式下操作您的编译器,以免在编译时而不是运行时出错。 –