将指针从一个函数传递到另一个函数
Posted
技术标签:
【中文标题】将指针从一个函数传递到另一个函数【英文标题】:Passing a pointer from one function to another 【发布时间】: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
【参考方案1】:
这是我最终得到的代码:
#include<stdio.h>
#include<math.h>
float * fun2(float J1, float J2, float J3, float t, float *x)
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] = x1 * x2 * (J2 - J3)/J1;
y[1] = x0 * x2 * (J3 - J1)/J2;
y[2] = x0 * x1 * (J1 - J2)/J3;
y[3] = (sin(x5) * x0 + cos(x5) * x1)/sin(x4);
y[4] = cos(x5) * x0 - sin(x0) * x1;
y[5] = x2 - (sin(x5) * cos(x4) * x0 + cos(x5) * x1)/sin(x4);
return y;
void fun1(float J1, float J2, float J3, float t, float *x, float h)
int i;
float x1[6];
for (i = 0; i < 6; i++)
x1[i] = x[i];
float k_1[6] = fun2(J1, J2, J3, t, x1);
float x2[6];
for (i = 0; i < 6; i++)
x2[i] = x[i] + (h * (4/27)) * k_1[i];
float k_2[6] = fun2(J1, J2, J3, t + h * (4/27), x2);
float x3[6];
for (i = 0; i < 6; i++)
x3[i] = x[i] + (h/18) * (k_1[i] + 3 * k_2[i]);
float k_3[6] = fun2(J1, J2, J3, t + h * (2/9), x3);
float x4[6];
for (i = 0; i < 6; i++)
x4[i] = x[i] + (h/12) * (k_1[i] + 3 * k_3[i]);
float k_4[6] = fun2(J1, J2, J3, t + h * (1/3), x4);
float x5[6];
for (i = 0; i < 6; i++)
x5[i] = x[i] + (h/8) * (k_1[i] + 3 * k_4[i]);
float k_5[6] = fun2(J1, J2, J3, t + h * (1/2), x5);
float x6[6];
for (i = 0; i < 6; i++)
x6[i] = x[i] + (h/54) * (13 * k_1[i] - 27 * k_3[i] + 42 * k_4[i] + 8 * k_5[i]);
float k_6[6] = fun2(J1, J2, J3, t + h * (2/3), x6);
float x7[6];
for (i = 0; i < 6; i++)
x7[i] = x[i] + (h/4320) * (389 * k_1[i] - 54 * k_3[i] + 966 * k_4[i] - 824 * k_5[i] + 243 * k_6[i]);
float k_7[6] = fun2(J1, J2, J3, t + h * (1/6), x7);
float x8[6];
for (i = 0; i < 6; i++)
x8[i] = x[i] + (h/20) * (-234 * k_1[i] + 81 * k_3[i] - 1164 * k_4[i] + 656 * k_5[i] - 122 * k_6[i] + 800 * k_7[i]);
float k_8[6] = fun2(J1, J2, J3, t + h, x8);
float x9[6];
for (i = 0; i < 6; i++)
x9[i] = x[i] + (h/288) * (-127 * k_1[i] + 18 * k_3[i] - 678 * k_4[i] + 456 * k_5[i]- 9 * k_6[i] + 576 * k_7[i] + 4 * k_8[i]);
float k_9[6] = fun2(J1, J2, J3, t + h * (5/6), x9);
float x10[6];
for (i = 0; i < 6; i++)
x10[i] = x[i] + (h/820) * (1481 * k_1[i] - 81 * k_3[i] + 7104 * k_4[i] - 3376 * k_5[i] + 72 * k_6[i] - 5040 * k_7[i] - 60 * k_8[i] + 720 * k_9[i]);
float k_10[6] = fun2(J1, J2, J3, t + h, x10);
for (i = 0; i < 6; i++)
x[i] = x[i] + h/840 * (41 * k_1[i] + 27 * k_4[i] + 272 * k_5[i] + 27 * k_6[i] + 216 * k_7[i] + 216 * k_9[i] + 41 * k_10[i]);
int main(int argc, char const *argv[])
float J1 = 4000;
float J2 = 7500;
float J3 = 8500;
float t_0 = 0;
float t_end = 40;
float h = 0.01;
float pi = 3.141592654;
int steps = t_end/h;
float x[6] = 0.1, -0.2, 0.5, 0, 0.5 * pi, 0;
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; h)
fun1(J1, J2, J3, t_0, x, h);
// S[i + 1][:] = x;
printf("%0.4f\t%0.4f\t%0.4f\t%0.4f\t%0.4f\t%0.4f\t%0.4f\n", t_0, x[0], x[1], x[2], x[3], x[4], x[5]);
t_0 = t_0 + h;
return 0;
它解决了我在传递东西时遇到的错误。
【讨论】:
我不知道这意味着什么:S[1][:] = x;
在编译此代码时,我会收到两页错误和警告godbolt.org/z/4sGdrc9Mx,您可能需要了解一下。以上是关于将指针从一个函数传递到另一个函数的主要内容,如果未能解决你的问题,请参考以下文章