I managed to condense the code where the problem occurs to this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct
{
double height;
double hello;
}Variables;
void information(Variables **Constants);
int main()
{
Variables *Constants=NULL;
information(&Constants); //Assigns values to the structure pointer
printf("Height %lf \n",Constants->height); //These print correctly only
printf("hello %lf \n",Constants->hello); //intermittently
return(0);
}
void information(Variables **Constants) //Assigns values structure pointer
{
Variables *consts,constants;
constants.height=10;
constants.hello=20;
consts=&constants;
*Constants=consts;
printf("Height %lf \n",constants.height); //These always print correctly
printf("hello %lf \n",constants.hello);
return;
}
From what i understand, this code should create a structure pointer in main, *Constants
. This pointer is then passed into the function using information(&Constants)
. In information()
another pointer is created and assigned a structure variable. The variable is then filled and the pointer is assigned to *Constants
, allowing the entire struct to then be passed back to main()
.
If i print the structure inside information()
, the values are correct. However if i print the values in main()
the values are sometimes correct, or they print random numbers. Any help in understanding this would be appreciated.