0

I have a task that requires handling a 4X4 matrix that is defined by typedef. the task is to create a calculator for declared matrixes in main and interpret the commands from the user that passed as simple words. the problem is that I trying to pass the matrixes between the function using it address.

int main(){

mat MAT_A,MAT_B,MAT_C,MAT_D,MAT_E,MAT_F; 
printf("MAT_A is at-%p\n",(void*)&MAT_A);
read_command(&MAT_A,&MAT_B,&MAT_C,&MAT_D,&MAT_E,&MAT_F);
}

in this function I interpret the matrixes that are to pass as value to the calculation function str_to_arg pass a mat* as the argument to add_mat function

void read_command(mat *a,mat *b,mat *c,mat *d,mat *e,mat *f){
add_mat((str_to_arg(a,b,c,d,e,f,strtok(NULL,","))),(str_to_arg(a,b,c,d,e,f,strtok(NULL,","))),(str_to_arg(a,b,c,d,e,f,strtok(NULL,","))));
printf("mat added at %p\n",(void*)a);
}

add_mat function receives 3 mat* as arguments

void add_mat(mat *a, mat *b , mat *c){
int i,j;
double sum;
for(i=0 ; i<MAT_LENGTH ; i++){
    for(j=0;j<MAT_LENGTH; j++){
        sum = *a[i][j]+ *b[i][j];
        *c[i][j]=sum;
        
    }
}   

printf("add succeed to matrix at adress %p\n",(void*)*c);
print_mat(*c);
}

as you can see Ive added 3 prints to follow the address at any given time, the addresses of the matrix at main and read_command are the same but the address that in add_mat is different.

I've tried many combinations include passing pointer to pointer and nothing seems to pass the right address into add_mat so the values of the actual matrix are'nt changing.

thanks for your help.

Niv Turk
  • 3
  • 2
  • 1
    The order in which function arguments are evaluated is unspecified. So the first letter you type could specify the 3rd argument, the next the 1st, the next the 2nd or any other order. Parse the input first in a defined order and then call `add_mat()`. – Goswin von Brederlow May 22 '22 at 20:19
  • `*a[i][j]` doesn't do what you think. It takes `a`, skips `i` `mat` ahead, then it looks up row number `j` in that `mat` and finally dereferences the row, meaning using the first column of that row. Did you mean `(*a)[i][j]`? – Goswin von Brederlow May 22 '22 at 20:23
  • the meaning is to look the i,j value of the matrix passed as "a". it doesn't seems to be the problem because the values the calculates are correct. the main problem is that the matrix (c) is not the actual address that required. if I execute Add_mat() directly from main this works just fine. (anyway probably I don't need to pass mat a , mat b as pointers because I just use the values they contain). – Niv Turk May 22 '22 at 20:33
  • If you execute `add_mat` from main then you don't have the UB from `read_command`. – Goswin von Brederlow May 22 '22 at 20:37
  • Please create a [mcve]. That means a complete program that compiles and runs without further edits, and with all irrelevant functionality removed. In particular, how is `str_to_arg` defined, and does it return a pointer to a writable `mat` with adequate lifetime? (And if it does, you probably have a memory leak anyway.) – Nate Eldredge May 22 '22 at 20:37

1 Answers1

1

Dereferencing of a pointer to 2d array should be done with:

(*a)[i][j]

The reason is that indexing operator [] has higher precedence than *.

The add_mat() function should be:

void add_mat(mat *a, mat *b , mat *c){
int i,j;
double sum;
for(i=0 ; i<MAT_LENGTH ; i++){
    for(j=0;j<MAT_LENGTH; j++){
        sum = (*a)[i][j]+ (*b)[i][j];
        (*c)[i][j]=sum;
        
    }
} 
tstanisl
  • 13,520
  • 2
  • 25
  • 40