//1.下面这个例子是计算数组中所有元素的和 //数组与指针的关系,整型是4个字节,a[] == *a, a[i] = *(a+i) #include <stdio.h> #define SIZE 10 int sum (int a[],int n); int main(){ int ar [SIZE] = {10,20,30,40,50,60,70,80,90}; long answer; answer = sum(ar, SIZE); //重点是这一句,ar为int [10]类型 printf("%lu\n",sizeof(answer)); //answer是long 型 8个字节 printf("answer=%lu\n",answer); //结果是450; return 0; } int sum (int a[]/***a*/, int n){//两种等价 int total = 0,i = 0; for (i = 0; i < n; i++) { // total += a[i]; //上面和下面的两种公式是等价, total += *(a+i); } return total; }
数组中所有元素的和.
时间: 2024-10-10 09:26:37