I am new to c and programming and not comfortable with dynamic memory allocation. However, it seems like for my purposes this is the only way:
I need to return a 2D
array of the size, defined in runtime from an external function into main-function:
void read(double (*data)[6], int size)
{
//do something and generate size and array[size*6]
for(i=0;i<size;i++){
for(j=0;j<6;j++)
data[i][j]=array[i*6+j];}
}
I call this function in main:
int main()
{
int size;
double *data= (double*) malloc(10000 *sizeof(double));
read(fname, data, size);//should supply me with a pointer to memory where data was stored
//I should be able to access the stored data somehow:
for (i = 0; i < size; ++i){
for(j=0;j<6;j++)
printf("%lf ", data[i][j]);printf("\n");
}
}
There is probably a whole bunch of mistakes in this code. I was trying to deliver the idea of what I want to do. Is there an implementation, or this is just nonsense? Thanks in advance!
Additional information: function read
reads data from file and formats it into an array of double. I really don't want to put it into main, I'd rather keep it as a library.