我正在撰寫一個代碼來使用跳躍方法集成一些 ODE,我必須提供一個函式,包括我的集成方法的導數,但是我得到一個“沒有被忽略的空值,因為它應該是錯誤的”。過去,當我將型別與函式指標弄錯時,我已經得到了這個,但據我所知,該函式沒有在任何地方分配任何東西,它應該只是執行?
所以我的源檔案在這里
#include "nrutil.h"
#include <math.h>
#include <stdio.h>
void leapvel(int n, double x[], double v[], double dt2, void (*derivs)(double, double [], double []))
{
double dvdt[n];
int i;
(*derivs)(n, x, dvdt);
for(i = 0; i < n; i )
v[i] = dt2*dvdt[i];
}
void output(int n, double x[], double v[], double t, double dt, void (*derivs)(double, double [], double []))
{
int i;
double vv[n], dvdt[n];
printf("%f", t);
for(i = 0; i < n; i )
printf(" %f", x[i]);
/*sync velocity*/
for (i = 0; i < n; i )
vv[i] = v[i];
if (t > 0)
{
(*derivs)(n,x,dvdt);
for (i = 0; i < n; i )
vv[i] -= 0.5*dt*dvdt[i];
}
printf("\n");
}
void leap(int n, double x[], double v[], double* t, double dt, void (*derivs)(double, double [], double []))
{
double dvdt[n];
int i;
for (i = 0; i < n; i )
x[i] = dt*v[i];
(*derivs)(n, x, dvdt);
for (i = 0; i < n; i )
v[i] = dt*dvdt[i];
*t = dt;
}
void leapfrog(int n, double x[], double v[], double t, double dt, double t_max, void (*derivs)(double, double [], double []))
{
/* integration time! */
/* parameters, the user has to set these, and to be honest it's going to be cumbersome either way in command line or just editing the code */
/* but since we have to run this twice! */
/* double t = 0, x[n] = {0}, v[n] = {1};
double t_max = 15, dt = 1;*/
/*this is the number of equations in derivs*/
/*int n = 1*/
int i;
output(n, x, v, t, dt, *derivs);
leapvel(n, x, v, 0.5*dt, *derivs);
while (t < t_max)
{
leap(n, x, v, &t, dt, *derivs);
output(n, x, v, t, dt, *derivs);
}
}
這是在一個短檔案中參考的,一旦我弄清楚這一點,我確實打算制作一個標題,因為它比簡單地包含源檔案更好,但現在這就是我所擁有的
#include <math.h>
void leapfrog(int n, double x[], double v[], double t, double dt, double t_max, void (*derivs)(double, double [], double []));
void derivs(int n, double x[], double dvdt[])
{
int i;
double accel = 0.0;
for(i = 0; i < n; i )
{
accel = -x[i];
dvdt[i] = accel;
}
}
int main()
{
/*frog goes {number of equations], {x initial conditions}, {v initial}, [t bottom], [t step], [t max], derivs(n,
* x[], dvdt[]) */
int n = 1;
double x[n];
double v[n];
x[0] = 0;
v[0] = 1;
double t = 0;
double dt = 1;
double t_max = 15;
double dvdt[n];
leapfrog(n, x, v, t, dt, t_max, *derivs(n, x, dvdt)); /*I get the error on this line*/
return 0;
}
我可能在這里遺漏了一些明顯的東西,但我不確定。
我試圖使該函式成為 void 指標型別函式,但這顯然很危險,并且具有未定義的行為,所以我現在有一個指向 void 函式的指標,但這會產生此錯誤。如果我洗掉引數,它會有其他奇怪的行為。我不知道如何使這項作業。正如上面宣告的那樣,洗掉引數顯然給出的引數太少,但是當我給它它想要的東西時,它會給出錯誤。
這意味著錯誤可能在首先顯示的源檔案中,但我似乎無法在其中找到它,據我所知,我沒有直接將它分配給任何東西,所以一切都應該沒問題。
uj5u.com熱心網友回復:
如果要將函式derivs作為引數傳遞給函式,只需撰寫derivs. 就像您傳遞任何其他變數的方式一樣。你不寫sin(double x);只是sin(x)。與功能相同。
在這里,您正在呼叫 derivs然后嘗試取消參考其不存在的回傳值:
leapfrog(n, x, v, t, dt, t_max, *derivs(n, x, dvdt))
編譯器物件,因為顯然沒有回傳值來取消參考。但真正的錯誤是撰寫函式呼叫而不僅僅是提供函式。你應該寫:
leapfrog(n, x, v, t, dt, t_max, derivs)
由于不明原因,編譯器可能不會抱怨這個不正確的函式呼叫:
output(n, x, v, t, dt, *derivs);
您試圖傳遞函式本身而不是指向函式的指標。如果您查看 的原型output,您會發現最后一個引數是一個指向函式的指標,因此您應該傳遞它。無論如何,沒有其他選擇,因為在 C 中函式不能用作物件;您只能使用指向函式的指標。因此該呼叫(以及其他類似呼叫)應寫為output(n, x, v, t, dt, derivs). leapfrog不出所料,這與應該呼叫的方式非常相似。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/532308.html
標籤:C指针颂
