用指针取代取下标来访问数组元素
计算a[0]*b[0]+a[1]*b[1]+....+a[n-1]*b[n-1].要求使用指针算术运算而不是取下标来访问数组元素.
程序代码:
#include<stdio.h>
#define N 5
double e[N];
double f[N];
double sum;
double inner_product( const double *a, const double *b, int n);
int main()
{
double s;
double *c ;
double *d ;
c =e;
d =f;
printf("请输入数组一:");
for (c = e; c < e +N; c++)
scanf("%lf", c);
printf("请输入数组二:");
for (d = f; d < f +N; d++)
scanf("%lf", d);
s = inner_product( c, d, N);
printf("%lf\n",s);
return 0;
}
double inner_product( const double *a, const double *b, int n)
{
int j;
for (a = e; a < e + n; a++) //最终修改结果
for (b = f; b < f + n; b++){
j = *a++ * *b;
sum += j;
}
return sum;
}
编译过了,但输入后求结果会闪退,虚心求教[ 本帖最后由 达尔文 于 2015-10-8 09:45 编辑 ]







擦.突然想到不能返回局部变量,好致命



.2.c.d是指针所有不用取址符.