码迷,mamicode.com
首页 > 编程语言 > 详细

c语言使用指针对int数组的求和

时间:2015-04-15 10:56:46      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
int sump(int *, int *);


int main(void)
{
    int array[] = {1, 3, 4, 7, 8};

    printf("total=%d\n", sump(array,array+5));
    return 0;
}

int sump(int * start, int * end)
{
    int total = 0;
    while(start<end)
        total += *start++;
    return total;
}

sump方法说明:

 1 数组名多持有的指针是该数组的首地址

 2 total += *start++;start指针的原型是int数组,每++一次移动一个int字节的长度,也就是取array数组下一个元素

  ++运算符和*具有同等优先级,同等优先级的运算符从右向左,所以是先++然后再*取值,最终移动到数组最后一个元素实现累加求和。

c语言使用指针对int数组的求和

标签:

原文地址:http://www.cnblogs.com/passedbylove/p/4427667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!