As the title explains I have a set of variables the objective is to print them in a file iteratively updating them every time, but for some reason I am no able to print the variable value, I am relatively new to C and finding pointers a little bit complicated to handle, my guess is the problem is about memory assignation.
Due to the length of it I present a shorter but equivalent version of my code:
#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);
}
*Clarification n value is meant to be much higher than 2 i just want it to be 2 for the program to run quickly so I can test it.
**If relevant somefunction is related to a runge-kutta step and the whole thing is part of a 4 body problem.
What I end up with is in fact a .txt file but instead of the values I want its all filled with "nan"s
int x; int* px; x = 42; *px = 42;So - if you have a pointer to some type, add a p to the variable name. If you want to access the type, you need as many "*" to de-reference as you have "p" in the name. If you had aint *** pppx;you still would know that to assign to it or use the value you would need 3 * to dereference.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;remove*. You do not need to use the pointer.int x = 42;, this means that there is memory at some address, which is large enough to hold an int. If you want the address, you write&x. If you have a pointer, it IS the address.int* px = &x;. If you want the value at that address, you dereference: `` int y = *px;``inis a somewhat confusing name for an output file.