首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

C中关于数组参数的传接

2012-08-27 
C中关于数组参数的传递编写一个对数组进行操作的函数,目的是返回数组内所用元素的和。?第一种形式:函数原型

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.

请按任意键继续. . .

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

热点排行