是否可以在将指针传递给函数时进行mu
的反转?有一个原始功能:
void norm1(float *F, float *mu, int N, int nav) {
for (int i=0; i<N; i++) {
int bl = i/nav;
F[i] *= 2*nav / mu[bl];
}
}
我希望有这样的东西:
void norm2(float *F, float *mu, int N, int nav) {
for (int i=0; i<N; i++) {
int bl = i/nav;
F[i] *= 2*nav * mu[bl];
}
}
当然,当我以这种方式调用它时:norm2(F, 1/mu, N, nav);
它不起作用,但我希望你会知道我想做什么
答案 0 :(得分:3)
使用:
// Allocate an array to hold the inverse of mu
float* mu_inv = malloc(N*sizeof(*mu_inv));
// Compute the inverse of mu and assign it to the newly allocated array.
for ( int i = 0; i < N; ++i )
{
mu_inv[i] = 1.0f/mu[i];
}
// Call norm2 with the inverse of mu
norm2(F, mu_inv, N, bl);
// Deallocate memory
free(mu_inv);