M
M. Moennigmann
Dear all:
I would like to write a function that opens a file, reads and stores
data into an 2d array, and passes that array back to the caller
(=main). The size of the array is not known before opening to the
file.
I fail to write a function that allocates memory for a 2d array and
returns it to main. I was trying to pass a pointer to the array back
to main, but main cannot access the data. The simplified code (no
opening of file, but only allocation) is attached below along with the
output.
I will appreciate any comment on what is going wrong. Thank you for
your help.
Martin
#include <stdlib.h>
#include <stdio.h>
int create2DimArray(int nx,
int ny,
double ***pointerToMyArray){
int i, j;
double **myArray;
// allocate memory for 2d array
myArray= (double **) malloc(nx* sizeof(double *));
for (i= 0; i< nx; i++){
myArray= (double *) malloc(ny* sizeof(double));
}
// fill array with funny numbers just for testing
for (i= 0; i< nx; i++){
for (j= 0; j< ny; j++){
myArray[j]= 1000.0* (double) i+ (double) j;
}
}
// print array to screen for testing/debugging
printf("in create2DimArray:\n");
for (i= 0; i< nx; i++){
for (j= 0; j< ny; j++){
printf(" i= %d, j= %d, %f\n", i, j, myArray[j]);
}
}
// return point er to array to caller
pointerToMyArray= &myArray;
return 0;
}
int main(){
int nx, ny, i, j;
double ***pointerToMyArray;
double **myArray;
nx= 3;
ny= 4;
// create 2d array
create2DimArray(nx, ny, pointerToMyArray);
myArray= *pointerToMyArray;
// try to print array to screen
printf("in main:\n");
for (i= 0; i< nx; i++){
for (j= 0; j< ny; j++){
printf(" i= %d, j= %d, %f\n", i, j, myArray[j]);
}
}
return 0;
}
-----------------------------------------
output
-----------------------------------------
in create2DimArray:
i= 0, j= 0, 0.000000
i= 0, j= 1, 1.000000
i= 0, j= 2, 2.000000
i= 0, j= 3, 3.000000
i= 1, j= 0, 1000.000000
i= 1, j= 1, 1001.000000
i= 1, j= 2, 1002.000000
i= 1, j= 3, 1003.000000
i= 2, j= 0, 2000.000000
i= 2, j= 1, 2001.000000
i= 2, j= 2, 2002.000000
i= 2, j= 3, 2003.000000
in main:
Segmentation fault
I would like to write a function that opens a file, reads and stores
data into an 2d array, and passes that array back to the caller
(=main). The size of the array is not known before opening to the
file.
I fail to write a function that allocates memory for a 2d array and
returns it to main. I was trying to pass a pointer to the array back
to main, but main cannot access the data. The simplified code (no
opening of file, but only allocation) is attached below along with the
output.
I will appreciate any comment on what is going wrong. Thank you for
your help.
Martin
#include <stdlib.h>
#include <stdio.h>
int create2DimArray(int nx,
int ny,
double ***pointerToMyArray){
int i, j;
double **myArray;
// allocate memory for 2d array
myArray= (double **) malloc(nx* sizeof(double *));
for (i= 0; i< nx; i++){
myArray= (double *) malloc(ny* sizeof(double));
}
// fill array with funny numbers just for testing
for (i= 0; i< nx; i++){
for (j= 0; j< ny; j++){
myArray[j]= 1000.0* (double) i+ (double) j;
}
}
// print array to screen for testing/debugging
printf("in create2DimArray:\n");
for (i= 0; i< nx; i++){
for (j= 0; j< ny; j++){
printf(" i= %d, j= %d, %f\n", i, j, myArray[j]);
}
}
// return point er to array to caller
pointerToMyArray= &myArray;
return 0;
}
int main(){
int nx, ny, i, j;
double ***pointerToMyArray;
double **myArray;
nx= 3;
ny= 4;
// create 2d array
create2DimArray(nx, ny, pointerToMyArray);
myArray= *pointerToMyArray;
// try to print array to screen
printf("in main:\n");
for (i= 0; i< nx; i++){
for (j= 0; j< ny; j++){
printf(" i= %d, j= %d, %f\n", i, j, myArray[j]);
}
}
return 0;
}
-----------------------------------------
output
-----------------------------------------
in create2DimArray:
i= 0, j= 0, 0.000000
i= 0, j= 1, 1.000000
i= 0, j= 2, 2.000000
i= 0, j= 3, 3.000000
i= 1, j= 0, 1000.000000
i= 1, j= 1, 1001.000000
i= 1, j= 2, 1002.000000
i= 1, j= 3, 1003.000000
i= 2, j= 0, 2000.000000
i= 2, j= 1, 2001.000000
i= 2, j= 2, 2002.000000
i= 2, j= 3, 2003.000000
in main:
Segmentation fault