C语言 数组如何在“for”循环中工作(C 语言)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21227259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do Arrays work in the "for" loop (C language)
提问by user2374254
i am relatively new in c programming, and programming in general.
我在 c 编程和一般编程方面相对较新。
1) I am trying to create an array named "input" that will be used for the user input (and the array will only use two integer elements).
1)我正在尝试创建一个名为“input”的数组,该数组将用于用户输入(并且该数组将仅使用两个整数元素)。
2) I want to use the for loop so it loop through my code 2 times, so i can duplicate the printf statement "Enter an integer," without me typing the printf statement multiple times. And then my scanf placeholder will be based my array "input."
2)我想使用 for 循环,所以它循环我的代码 2 次,所以我可以复制 printf 语句“输入一个整数”,而无需多次输入 printf 语句。然后我的 scanf 占位符将基于我的数组“输入”。
3) Then i want to add those two numbers together for the sum.
3)然后我想将这两个数字相加求和。
The problem:When the user enters a number, for example, 1 and then 1 again, the index at input[0] is 1 but then the index at 1 for input[1] is 2...
问题:当用户输入一个数字时,例如,1,然后又是 1,input[0] 处的索引是 1,但是 input[1] 1 处的索引是 2...
I will use addition to try and explain. When i try to output the numbers in addition (integer[0] is 1, integer[1] is 2 and my variable which adds them together is "total")i get this: 1+2=2.
我将使用加法来尝试解释。当我尝试另外输出数字时(整数 [0] 是 1,整数 [1] 是 2 并且我将它们加在一起的变量是“总计”)我得到这个:1+2=2。
So why is input[0] correct, but input[1] seems to be adding the two user inputs together and storing it in there?
那么为什么 input[0] 是正确的,但 input[1] 似乎将两个用户输入加在一起并将其存储在那里?
Here is my code
这是我的代码
int main()
{
int input[1];
for(int i = 0; i < 2; i++)
{
printf("Please enter an integer: ");
scanf("%d", &input[i]);
}
int total = input[0]+input[1];
printf("%d + %d = %d ", input[0], input[1], total);
}
Thanks in advance, and i hope you understand me. Sorry i'm a noob, learning the basics so i can become advanced.
提前致谢,我希望你能理解我。对不起,我是菜鸟,学习基础知识,这样我才能进阶。
采纳答案by SoulRayder
You should use
你应该使用
int input[2];
since you want an array of two integers. Otherwise , it will just allocate one integer array for you, and accessing input[1] in that case will not work properly.
因为你想要一个包含两个整数的数组。否则,它只会为您分配一个整数数组,在这种情况下访问 input[1] 将无法正常工作。
Also try this to extend that to adding n integers.
也可以尝试将其扩展为添加 n 个整数。
int main()
{
int input[1];
int total = 0;
int n; //To count number of elements to add:
printf("Please enter the number of elements to add: ");
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
printf("Please enter integer %d: ",(i+1));
scanf("%d", &input[i]);
}
for(int i = 0; i < n; i++)
{
total = total +input[i];
}
printf("The sum is = ", total);
}
So in your array, as indicated in the loops I have used, you will be accessing a singleelement of inputarray in each iteration by making use of its indexwhich you also increment in each iteration.
因此,在您的数组中,如我使用的循环所示,您将在每次迭代中访问输入数组的单个元素,方法是使用它的索引,您也在每次迭代中递增该索引。
In your case, there are only twoelements, so it requires two iterations only.
在您的情况下,只有两个元素,因此只需要两次迭代。
回答by Ravindra Gupta
this line
这条线
int input[1];
整数输入[1];
will create array of size one only. And i can see you are trying to access second element of array also.
将仅创建大小为 1 的数组。而且我可以看到您也在尝试访问数组的第二个元素。
so just replace your this line with
所以只需将您的这一行替换为
int input[2];
整数输入[2];
this will solve your problem. or put this whole thing
这将解决您的问题。或者把这整件事
int main()
{
int input[2];
for(int i = 0; i < 2; i++)
{
printf("Please enter an integer: ");
scanf("%d", &input[i]);
}
int total = input[0]+input[1];
printf("%d + %d = %d ", input[0], input[1], total);
}
回答by Sajad Karuthedath
Use like this
像这样使用
int main()
{
int input[2]; //initializes array of size 2,which can contain maximun 3-->(0,1,2)
for(int i = 0; i < 2; i++)
{
printf("Please enter an integer: ");
scanf("%d", &input[i]);
}
int total = input[0]+input[1];
printf("%d + %d = %d ", input[0], input[1], total);
}
ORuse i<=2.
或使用i<=2.
int main()
{
int input[1]; //initializes array of size 1 which can contain -->(0,1),or use i<=2
for(int i = 0; i <= 2; i++)
{
printf("Please enter an integer: ");
scanf("%d", &input[i]);
}
int total = input[0]+input[1];
printf("%d + %d = %d ", input[0], input[1], total);
}

