为什么下面的代码不需要&符号?

moh*_*moh 3 c pointers scanf

我知道在 C 编程中,' scanf ' 与 ' & ' 一起用于除字符串之外的所有变量类型(int、float、char、..)。这是我的程序代码。在' 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);
}
Run Code Online (Sandbox Code Playgroud)

Adr*_*ica 5

因为您已声明a数组,所以使用该变量名称的表达式本身通常会“衰减”到指向数组第一个元素的指针什么是数组衰减? - 但另请参阅 Eric 添加的优秀评论Postpischil 为例外)。这类似于使用char[]字符串,正如您正确指出的那样,&在将其作为参数传递给scanf.

添加ia数组的这个“基地址”将给出数组的“第 i 个元素”的地址(即指向 的指针)。

这是一个关于C指针运算的体面教程,您可能会发现它很有用。