数组求和,C
#include<stdio.h>
int addarray(int array[],int n);
int main()
{
int data[] = {0,1,2,3,4,5,6,7,8,9};
int size = sizeof(data)/sizeof(data[0]);
printf("data:%d/n",sizeof(data));
printf("sum:%d/n",addarray(data,size));
return(0);
}
int addarray(int array[],int n)
{
int sum=0;
int i;
printf("array:%d/n",sizeof(array));
for(i=0;i<n;i++)
{
sum += array[i];
}
return(sum);
}
输出为:
data:40
array:8
sum:45
数组求和,指针,C
#include<stdio.h>
int addarray(int *array,int n);
int main()
{
int data[] = {0,1,2,3,4,5,6,7,8,9};
int size = sizeof(data)/sizeof(data[0]);
printf("data:%d/n",sizeof(data));
printf("sum:%d/n",addarray(data,size));
return(0);
}
int addarray(int *array,int n)
{
int sum=0;
int i;
printf("array:%d/n",sizeof(array));
for(i=0;i<n;i++)
{
sum+=*array++;
}
return(sum);
}
数组求和,C++
#include <iostream>
int addarray(int *array,int n);
int main()
{
int data[] = {0,1,2,3,4,5,6,7,8,9};
int size = sizeof(data)/sizeof(data[0]);
std::cout << "sum:" << addarray(data,size) << std::endl;
return(0);
}
int addarray(int *array,int n)
{
int sum=0;
int i;
for(i=0;i<n;i++)
{
sum+=*array++;
}
return(sum);
}