我试图在一个较小的示例中执行此操作,以便可以在较大的示例中执行该操作。
这是我尝试的代码:
int counts[0];
int numbers[] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < 10; i++)
counts[0] += numbers[i];
printf("%d ", counts[0]);
这应该产生55,但我的输出仅为14。我如何设置numbers
数组存在问题,但是我不太确定如何解决此问题。感谢您的任何帮助,谢谢。
答案 0 :(得分:2)
int counts[0]; // This declares an array that holds ZERO elements! No place to store a value.
int numbers[] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < 10; i++)
counts[0] += numbers[i];
printf("%d ", counts[0]);
要修复它:
int counts = 0; // Declare a variable, and start it at 0
int numbers[] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0; i < 10; i++)
counts += numbers[i];
printf("%d ", counts);