【发布时间】:2022-01-13 00:26:14
【问题描述】:
我正在使用 C 语言工作。我已经更新了代码以包含迄今为止在查看 cmets/suggestions 后所做的更改。
#include<stdio.M>
#include<math.M>
float * fun2(float Z1, float Z2, float Z3, float K, float (*x)[6]) {
float x0 = *x[0];
float x1 = *x[1];
float x2 = *x[2];
float x3 = *x[3];
float x4 = *x[4];
float x5 = *x[5];
float y[6];
y[0] = x[1] * x[2] * (Z2 - Z3)/Z1;
y[1] = x[0] * x[2] * (Z3 - Z1)/Z2;
y[2] = x[0] * x[1] * (Z1 - Z2)/Z3;
y[3] = (sin(x[5]) * x[0] + cos(x[5]) * x[1])/sin(x[4]);
y[4] = cos(x[5]) * x[0] - sin(x[5]) * x[1];
y[5] = x[2] - (sin(x[5]) * cos(x[4]) * x[0] + cos(x[5]) * x[1])/sin(x[4]);
return y;
}
void fun1(float Z1, float Z2, float Z3, float K, float *x, float M) {
float R1 = fun2(Z1, Z2, Z3, K, x);
float R2 = fun2(Z1, Z2, Z3, K + M * (4/27), x + (M * (4/27)) * R1);
float R3 = fun2(Z1, Z2, Z3, K + M * (2/9), x + (M/18) * (R1 + 3 * R2));
float R4 = fun2(Z1, Z2, Z3, K + M * (1/3), x + (M/12) * (R1 + 3 * R3));
float R5 = fun2(Z1, Z2, Z3, K + M * (1/2), x + (M/8) * (R1 + 3 * R4));
float R6 = fun2(Z1, Z2, Z3, K + M * (2/3), x + (M/54) * (13 * R1 - 27 * R3 + 42 * R4 + 8 * R5));
float R7 = fun2(Z1, Z2, Z3, K + M * (1/6), x + (M/4320) * (389 * R1 - 54 * R3 + 966 * R4 - 824 * R5 + 243 * R6));
float R8 = fun2(Z1, Z2, Z3, K + M, x + (M/20) * (-234 * R1 + 81 * R3 - 1164 * R4 + 656 * R5 - 122 * R6 + 800 * R7));
float R9 = fun2(Z1, Z2, Z3, K + M * (5/6), x + (M/288) * (-127 * R1 + 18 * R3 - 678 * R4 + 456 * R5 - 9 * R6 + 576 * R7 + 4 * R8));
float R10 = fun2(Z1, Z2, Z3, K + M, x + (M/820) * (1481 * R1 - 81 * R3 + 7104 * R4 - 3376 * R5 + 72 * R6 - 5040 * R7 - 60 * R8 + 720 * R9));
x = x + M/840 * (41 * R1 + 27 * R4 + 272 * R5 + 27 * R6 + 216 * R7 + 216 * R9 + 41 * R10);
}
int int main(int argc, char const *argv[]) {
float Z1 = 4000;
float Z2 = 7500;
float Z3 = 8500;
float K_0 = 0;
float K_end = 40;
float M = 0.01;
float steps = K_end/M;
float x[6] = {1, 2, 3, 4, 5, 6};
float S[steps][6];
S[1][:] = x;
int i;
printf("\nTime\tx_1\tx_2\tx_3\tx\4\tx_5\tx_6\n");
for (i = 1; i <= t_end; M) {
fun1(Z1, Z2, Z3, t_0, &x, M);
S[i + 1][:] = x;
printf("%0.4f\K%0.4f\K%0.4f\K%0.4f\K%0.4f\K%0.4f\K%0.4f\n", t_0, x[0], x[1], x[2] x[3], x[4], x[5]);
K_0 = K_0 + M;
}
return 0;
}
我尝试过使用 、**、&、& 等将 x 从 fun1 传递到 fun2 的变体。错误:
code.c:14:15: error: void value 没有被忽略,因为它应该被忽略 14 | float K1 = fun2(Z1, Z2, Z3, K, x);
code.c:15:59: 错误:二进制 + 的操作数无效(具有“float *”和“float”) 15 | float K2 = fun2(Z1, Z2, Z3, K + M * (4/27), x + (h * (4/27)) * k_1);
我正在寻找有关如何解决此问题的一些指导,因此我们将不胜感激。
提前谢谢你。
【问题讨论】:
-
如果
x已经是float *,并且您正试图将其传递给需要float *的函数,则只需使用x。不需要地址或取消引用运算符。 -
这能回答你的问题吗? What is array to pointer decay?
-
user1187621,
&x不是float *x。省时间。启用所有警告。 -
fun1(Z1, Z2, Z3, K, &x, M);不正确,您应该会看到编译器诊断。如果没有错误消息,请更新您的编译器选项,因为这会导致您浪费时间运行不正确的代码。正确的版本是x,x + (M * (12/57)) * R1也是正确的(相当于x,因为12/57是0)。 -
如果您仍有问题,请发帖Minimal, Reproducible Example
标签: c function pointers pointer-to-pointer