我知道在C编程中,对于除字符串以外的所有可变类型(int,float,char,..),'scanf'与'&'一起使用。这是我的程序代码。在'scanf'前面,为什么不需要'&'?我可以进一步了解scanf吗?
#include <stdio.h>
#define M 10
int main()
{
int i, n, sum = 0;
int a[M];
do{
printf("Input a positive number less than %d \n",M);
scanf("%d", &n);
}while (!(n >0 && n < M));
printf("Input %d numbers \n",n);
for(i=0; i<n ; i++){
scanf("%d",(a+i));
sum += *(a+i);
}
printf("Sum = %d \n",sum);
}
因为您已将a
声明为array,所以使用该变量的名称本身将成为数组第一个元素的pointer(这类似于使用[C0 ]字符串,在将char[]
运算符作为参数传递给&
时,也不需要使用该字符串。
将scanf
添加到i
将为地址(即指向]的指针)数组的第i个元素。
这里是关于a
的不错的教程,您可能会觉得有用。
在c中的变量之前使用的[pointer arithmetic in C运算符返回该变量的地址。数组的基数已经是您想要的地址。 &
就像您正在使用指针算术,并通过i