Two options to return value from the function.
#include <stdio.h>
double dot (const double *v_1, const double *v_2)
{
return v_1[0] * v_2[0] + v_1[1] * v_2[1];
}
void sum (double *v_1, double *v_2, double *v_out)
{
v_out[0] = v_1[0] + v_2[0];
v_out[1] = v_1[1] + v_2[1];
}
int main ()
{
double *r;
double *t;
r = malloc(sizeof(double)*2);
t = malloc(sizeof(double)*2);
r[0] = 1; r[1] = 2;
double rho_new = 0;
rho_new = dot(r, r, rho_new);
printf("rho_new = %lf
", rho_new);
sum (r, r, t);
printf("t = %lf %lf
", t[0], t[1]);
}
#include <stdio.h>
void dot (const double *v_1, const double *v_2, double *s_out)
{
*s_out = v_1[0] * v_2[0] + v_1[1] * v_2[1];
}
void sum (double *v_1, double *v_2, double *v_out)
{
v_out[0] = v_1[0] + v_2[0];
v_out[1] = v_1[1] + v_2[1];
}
int main ()
{
double *r;
double *t;
r = malloc(sizeof(double)*2);
t = malloc(sizeof(double)*2);
r[0] = 1; r[1] = 2;
double rho_new = 0;
dot (r, r, &rho_new);
printf("rho_new = %lf
", rho_new);
sum (r, r, t);
printf("t = %lf %lf
", t[0], t[1]);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…