C中关于数组参数的传递
编写一个对数组进行操作的函数,目的是返回数组内所用元素的和。
?
第一种形式:函数原型为int sum(int ar[],int n)
?
#include<stdio.h>#include<string.h>#define SIZE 4int sum(int *start,int * end);//void swap(int * a,int * b);int main(int argc,char *argv[]){int arrays[SIZE] = {1,2,3,4};long answer;answer = sum(arrays,arrays + SIZE);printf("total:%ld.\n",answer);return 0;}int sum(int *start,int *end){int total = 0; //要初始化while(start < end){total +=*start;start++;}return total;}?
?
运行结果:
?
total:10.
请按任意键继续. . .
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?