I have a trouble with my program and I don't know why.
Can you help me ?
I have MPLAB 8.92 + CSS and I have a PICKIT3.
So I would like to sum two matrices (and later, program a matrix multiplication).
My first code is about Integer matrices. It works very well.
Code:
#include <18F45K20.h>
void sum(int **mat1,int **mat2, int **res){
int i=0;
int j=0;
int k=0;
for(i=0;i<2;i++){
for(j=0;j<2;j++){
*(res+(1*i)+j)=*(mat1+(1*i)+j)+*(mat2+(1*i)+j);
}
}
}
int m1[2][2]={{15,30},{20,21}};
int m2[2][2]={{1,0},{0,1}};
int result[2][2];
void main()
{
sum(m1,m2,result);
while(1);
}
I get : result[2][2]={{16,30},{20,22}}, that's correct.
Now, I would like to do the same think but not with Integer but with Double.
So, I change the "int" by "double" : it doesn't work.
Alright, thanks for your answer ! Pointers are something new to me...
By using, "int *x" or "double *x", it works perfectly !
Just one more question :
Why can we say that : matrice[j] is equivalent to *(matrice+(number of columns*i)+j). I don't get the point.
Can you explain me ?
Imagine all the elements of the matrix are stored in memory, linerarly, one by one. When you add 1 to a pointer, it shifts one position to the right, so if "matrice" pints to an element of the array, (matrice+1) will point through the next element. "matrice" points to the first element, (matrice+1) points to the second, (matrice+n) points to the (n-1)th element. In such use, n is often called "offset". Each element of the matrix has its own offset. You simply calculate the offset where (i,j) element is - (number of columns*i)+j and add it to "matrice". Now it points to the desired element. *(something) means you want to take an element pointed by "something". When you apply it to the pointer to an element, it fetches the element pointed to by the pointer.
Note, that if you use one-dimentional array, it's up to you wether you want it by rows or by columns (number of rows*j)+i will work just as well as (number of columns*i)+j. You only need to make sure that there's enough memory for the matrix at the place where the pointer points to.
Sorry for taking so long to anwser. Something came up and I had to fix it first.
Anyway, thanks for your explanation. Some parts are already more understable to me. But I need more clarifications for other parts.
Imagine all the elements of the matrix are stored in memory, linerarly, one by one. When you add 1 to a pointer, it shifts one position to the right, so if "matrice" pints to an element of the array, (matrice+1) will point through the next element. "matrice" points to the first element, (matrice+1) points to the second, (matrice+n) points to the (n-1)th element. In such use, n is often called "offset".
I understand this part very easily if we talk about an one-dimentional array. In fact, (matrice+n) can be compared to (matrice[n]).
I can picture that in my head
However, if we talk about matrices (two-dimentional arrays), I am getting confused a little.
Indeed, how can all the elements of the matrix be stored in memory, linerarly, one by one ?
Let's consider a matrix with two columns. Does it means the second column would be under the first one in the memory ?
*(something) means you want to take an element pointed by "something". When you apply it to the pointer to an element, it fetches the element pointed to by the pointer.