Hi, all. I've tried and tried to get my head around this, and feel I am almost there, but I'm getting so confused with how many '*' I need! I have a function that takes as input the string of a directory containing a data file, an int and pointers to two uninitialized 2D arrays. The function reads the data file and then allocates memory and fills the arrays accordingly.
This code is probably quite wrong, but the idea is:
Code:
void main()
{
double **Array1;
int **Array2;
int dimension1;
char DirWork[100], buff[100];
f_ReadData(DirWork, dimension1, &Array1, &Array2);
sprintf(buff,"%lf",Array1[0][0]); // Causes segmentation fault
printf(buff);
}
and
Code:
void f_ReadData(char *DirWork, int dimension1, double ***Array1ptr, int ***Array2ptr)
{
int ct, ct2;
double **Array1 = *Array1ptr;
int **Array2 = *Array2ptr;
char FullDirArray1[100], FullDirArray2[100];
FILE *d_Array1, *d_Array2;
sprintf(FullDirArray1,"%s%s",DirWork,"Array1.dat");
sprintf(FullDirArray2,"%s%s",DirWork,"Array2.dat");
d_Array1=fopen(FullDirArray1,"r");
d_Array2=fopen(FullDirArray2,"r");
fscanf(d_Array1,"%d", &dimension1);
Array1 = dmatrix(0,dimension1-1,0,3); // allocates memory to Array1 (dimension1 x 3) elements, using nrutil
Array2 = imatrix(0,dimension1-1,0,3); // allocates memory to Array2 (dimension1 x 3) elements, using nrutil
for(ct=0; ct<dimension1; ct++)
{
for(ct2=0; ct2<3; ct2++)
{
fscanf(d_Array1, "%lf", &Array1[ct][ct2];
fscanf(d_Array2, "%d", &Array2[ct][ct2];
}
}
fclose(d_Array1);
fclose(d_Array2);
Array1ptr = &Array1;
Array2ptr = &Array2;
}
I've missed out error handling here, but I do have some of that in place... not that it's helping. I'm getting a segmentation fault when I try to access the arrays from the main function.
If anyone could help, I'd really appreciate it... I'm seeing *stars! Thank you!