Hey,
Here is a sample of code I will be refering to:
Code:
#include <stdio.h>
#include "misc.h"
int main(void){
double *x,*y;
data *data_ptr;
char file[100];
printinfo();
printf("Enter data file:");
scanf("%s",file);
//getdata returns pointer to data structure
data_ptr = getdata(file);
x = data_ptr[0].x;
y = data_ptr[0].y;
printf("%u\n",data_ptr[0].larray);
printf("Value of X = %lf \nValue of Y = %lf\n",*x,*y);
return 0;
}
and the structure looks like this
Code:
typedef struct {
unsigned int larray;
double *x;
double *y;
}data;
So what I am trying to do is, I assign data_ptr the address on the data structure. I then want to initialize the variables x and y to two of the members of the structure but this is not working. Can you point me in the right direction on where I may have gone wrong.
See below for getdata function:
Code:
data * getdata(const char file[])
{
int numLines = 0;
double x[200],y[200];
data *data_ptr;
char line[80];
FILE *file_data;
//open file
file_data = fopen(file, "r");
if (file_data == NULL)
printf("Could not open file");
//count number of lines in file
while ( fgets(line, 80, file_data) != NULL) numLines++;
//allocate memory for structure
data_ptr = malloc(sizeof(data) * numLines);
//initilize values for structure elements
for(int count = 0; count <= numLines; count++){
fscanf(file_data, "%lf %lf", &x[count], &y[count]);
data_ptr[count].larray = numLines;
data_ptr[count].x = &x[count];
data_ptr[count].y = &y[count];
}
printf("Address of data_ptr in misc = %i\n",data_ptr);
//return data structure pointer address to main
return data_ptr;
}
and the file I can referring too will just look like something like this (plain text file)
1.87100000e+03 4.16000000e+00
1.87200000e+03 4.69000000e+00
1.87300000e+03 5.40000000e+00
1.87400000e+03 5.80000000e+00
1.87500000e+03 5.60000000e+00
1.87600000e+03 5.55000000e+00
1.87700000e+03 4.17000000e+00
1.87800000e+03 4.40000000e+00
1.87900000e+03 4.05000000e+00
If you need any more information just ask.
Thank you