I wrote a code and I don't know how it works. The scenario is the dynamic allocation of a 2D array in a function which reads an external file
void load_array(double **(&a), int *rows, int *random_val)
{
//I know the number of columns e.g. 3
int i,j;
double temp;
FILE *fp;
//Assign a value to random variable
*random_val=5;
//Read file and determine number of lines(rows) block of code
{
//Code goes here
}
a=(double **)calloc((*rows),sizeof(double *);
for(i=0;i<(*rows);i++)
a[i]=(double **)calloc(2,sizeof(double *); //Only 2 collumns
i=0;
fp=fopen("input","r");
fscanf(fp,"%lf\t%lf\n",&a[0][0],&a[0][1])
while(!feof(fp))
{
i=i+1;
fscanf(fp,"%lf\t%lf\n",&a[i][0],&a[i][1]);
}
*rows=i;
fclose(fp);
}
int main(void)
{
double **A;
int row_count;
double temp;
load_array(A,&row_count, &temp);
return 0;
}
I can't understand the following points:
- Do I have to use the values in the function carrying the asterisk?
For example
for(i=0;i<rows;i++)
instead offor(i=0;i<(*rows);i++)
andrandom_val=5;
instead of*random_val=5;
? - Why I declare the function this way, using
**(&a)
for the array? Moreover I can't understand why to pass the array this way (without&
).
My pointer knowledge is somewhat limited and I'm confused. Please do not propose std::vector
. I would like to clarify first this point.