0
int main(){

int f,d;
float** a1, **a2,**p,**t;
read_from_file(a1,a2);
/*DEBUG PRINT AGAIN MATRIX A1 */
for(f = 0; f<4; f++)
    for(d = 0; d<4; d++)
        printf("a[%d][%d] = %f\n",f,d,a1[f][d]);
}


void read_from_file(float** a, float** b){
    a = my_malloc(4,4);
    b = my_malloc(4,4);
    int temp;
    FILE* fd;
    fd = fopen("matrici.txt","r");
    if(fd == NULL){
         perror("errore opening file\n");
         exit(1);
    }
    int i,j;
    for(i=0; i<4; i++)
        for(j=0; j<4; j++){
            fscanf(fd,"%d", &temp);
            a[i][j]= (float)temp;
        }
    for(i=0; i<4; i++)
        for(j=0; j<4; j++){
            fscanf(fd,"%d", &temp);
            b[i][j]= (float)temp;
        }
    fclose(fd);

    /* DEBUG PRINT MATRIX "A1" */
    for(i = 0; i<4; i++)
        for(j = 0; j<4; j++)
        printf("a[%d][%d] = %f\n",i,j,a[i][j]);
}

ok hello everybody, while i was coding a program that should had to read from file some floating values and store them into that matrix, an inusual bug occurred: when i print my matrix in the function " read from file" the values stored in it are:

a[0][0] = 3.000000
a[0][1] = 1.000000
a[0][2] = -1.000000
a[0][3] = 0.000000
a[1][0] = 0.000000
a[1][1] = 7.000000
a[1][2] = -3.000000
a[1][3] = 0.000000
a[2][0] = 0.000000
a[2][1] = -3.000000
a[2][2] = 9.000000
a[2][3] = -2.000000
a[3][0] = 0.000000
a[3][1] = 0.000000
a[3][2] = 4.000000
a[3][3] = -10.000000

but when i print again the same matrix in the main function ( after the calling of read_from_file) the values stored in it are:

a[0][0] = 53806348.000000
a[0][1] = 14144784983268524032.000000
a[0][2] = 208613343232.000000
a[0][3] = 15659650322576441344.000000
a[1][0] = 13841544192.000000
a[1][1] = 0.048908
a[1][2] = 71857615689329949954782789632.000000
a[1][3] = 202218044416841482240.000000
a[2][0] = 3664429973504.000000
a[2][1] = 15151608880334635008.000000
a[2][2] = 234149008.000000
a[2][3] = 55906620.000000
a[3][0] = 14358698268987228160.000000
a[3][1] = 3157.142334
a[3][2] = 15151608880334635008.000000
a[3][3] = 13.265934

thanks for the help :)

Shinon
  • 1
  • 4
  • Please note that these are not arrays, but slow, segmented look-up tables, for which you have no use. There is no need to make the program extra slow when it could be fast. See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Mar 02 '18 at 12:34

1 Answers1

1

Function's parameters are its local variables. You can imagine a call of the function read_from_file and its definition the following way

float** a1, **a2,**p,**t;
read_from_file(a1,a2);

//...

void read_from_file( /*float** a, float** b */){
    float **a = a1;
    float **b = a2;

    a = my_malloc(4,4);
    b = my_malloc(4,4);
    //...

So within the function the original arguments a1 and b1 are not changed. The function deals with copies of the values of the original arguments.

You should pass the arguments by reference that is indirectly through pointers if you want to change them within the function.

For example

float** a1, **a2,**p,**t;
read_from_file( &a1, &a2 );

//...

void read_from_file( float ***a, float ***b){

    *a = my_malloc(4,4);
    *b = my_malloc(4,4);
    //...
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • thank you, problem solved!!!!!!! i was thinking that as the arrays the pointers automatically were passed as reference! – Shinon Mar 02 '18 at 12:24