在 C 中的循环中为变量和 fprintf 赋值

assigning values to variables and fprint in a loop in C

正如标题所解释的那样,我有一组变量 objective 是将它们打印在一个文件中,每次都迭代更新它们,但由于某种原因我无法打印变量值,我是C 相对较新并且发现指针处理起来有点复杂,我猜问题是关于内存分配的。

由于它的长度,我提供了一个更短但等效的代码版本:

#include <stdlib.h>
#include <math.h>
#include <stdio.h>

int main()
{
    int n = 2;
    FILE *in;
    char filename1[30] = "positions.txt";
    double *x_tp, *y_tp, *z_tp;
    double *x_tf, *y_tf, *z_tf;
    *x_tp = 1.0;
    *y_tp = 0.0;
    *z_tp = 0.0;
    in = fopen(filename1, "w");
    fprintf(in,"%lf \t %lf \t %lf\n",x_tp,y_tp,z_tp);
    fclose(in);
    in = fopen(filename1, "w");
    for (i = 1; i <= n; i++)
    {
        *x_tf = x_tp + "somefunctionx";
        *y_tf = y_tp + "somefunctiony";
        *z_tf = z_tp + "somefunctionz";
        fprintf(in,"%lu \t %lu \t %lu\n",x_tf,y_tf,z_tf);
        x_tp =  x_tf;
        y_tp =  y_tf;
        z_tp =  z_tf;
    }
    fclose(in);
}

*澄清 n 值意味着比 2 高得多,我只是希望它是 2 以便程序快速 运行 以便我可以测试它。

**如果相关的某些功能与 runge-kutta 步骤相关,并且整个事情是 4 body 问题的一部分。

我最终得到的实际上是一个 .txt 文件,但我希望它全部填充 "nan"s

而不是值

首先,您根本不需要在这里将变量声明为指针。

double x_tf, ...; 

会更合适。那么你可以这样写:

x_tf = 1.0;

及以后:

printf("%f", x_tf); 

而且任何地方都没有指针问题。

如果您现在有一个具有 OUT 或 INOUT 语义参数的函数,例如

void RungeKuttaStep( double *pxs, double *pys ); // whatever

这在语义上意味着他们不仅需要该值而且还打算将新值写入这些变量,您将其称为

double xs = 1.0;
double ys = 1.0;
RungeKuttaStep( &xs, &ys );
printf( "now xs = %f and ys = %f\n", xs, ys );

您可能尝试实现的是交替使用变量。 每次调用后,您想将上一步的结果用作下一次调用的新输入。
为此,您可以这样写:

double a1,b1,c1; // lazily renamed your variables, mean me
double a2,b2,c2;

// probably in a loop
a1 = 1.0; b1 = 1.0; c1 = 1.0;
a2 = a1 + RungeKutta1(a1,b1,c1);
b2 = b1 + RungeKutta2(a1,b1,c1);
c2 = c1  + RungeKutta3(a1,b1,c1);
a1 = a2; b1 = b2; c1 = c2;
// probably end of loop

double 有 8 个字节的大小和一个指针,具体取决于您的 OS 和 build 有 4 或 8 个字节的数据。不使用指针你不会注意到速度上的差异。

所有变量

double *x_tp, *y_tp, *z_tp;
double *x_tf, *y_tf, *z_tf;

是指向双打的指针 如果您愿意,这意味着他们正在寻找双打地址。 在代码中,您没有为要指向的指针声明双精度数。

这是指针的例子

#include <stdlib.h>
#include <math.h>
#include <stdio.h>

int main()
{
 int n = 2;
 FILE *in;
 char filename1[30] = "positions.txt";
 double x_tp, y_tp, z_tp;

 /*Initialize Pointers */
 double *x_tf = &x_tp, *y_tf = &y_tp, *z_tf = &z_tp;

 /* Assigns the doubles to the pointer variables & initializes variables. */
  x_tp = 1.0;
  y_tp = 0.0;
  z_tp = 0.0;

 in = fopen(filename1, "w"); /* Opens and prints variables to file */
 fprintf(in, "%lf \t %lf \t %lf\n", x_tp, y_tp, z_tp);
 fclose(in);
 in = fopen(filename1, "a"); /*Appends the new numbers to the file -- "w" will rewrite the file entirely. */
 for (int i = 1; i <= n; i++)
 {
     x_tp = 2.0; /*Example of updating the values at x_tp -- etc. Pointers will have new values. */
     y_tp = 22.0;
     z_tp = 1.0;
     /* Assigns doubles to pointers then prints the value at that pointer */
        fprintf(in, "%lf \t %lf \t %lf\n", *x_tf, *y_tf, *z_tf); /* Prints Values at each pointer -- tells the compiler to go to that pointer and print the value. */

 }
 fclose(in);

}

您的指针需要指向某个 double。 如果在声明语句中进行初始化,您还可以使用地址初始化指针。

循环的读/写中的 "w" 会覆盖整个文本文件; "a" 附加和添加到文本文件。

与其说是内存,不如说是内存;你只需要给你的指针地址。 如果您想在代码中打印 "somefunctionx" 。 您可以只分配新值然后执行:

 x_tp = 2.0; /*Updates pointer */
 fprintf(in, "somefunctionx : ", *x_tf);

您也可以只为每个后续变量添加“\n”。