Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between matrix implementation in C

Tags:

c

matrix

I created two 2D arrays (matrix) in C in two different ways.
I don't understand the difference between the way they're represented in the memory, and the reason why I can't refer to them in the same way:

scanf("%d", &intMatrix1[i][j]); //can't refer as  &intMatrix1[(i * lines)+j])

scanf("%d", &intMatrix2[(i * lines)+j]); //can't refer as &intMatrix2[i][j])

What is the difference between the ways these two arrays are implemented and why do I have to refer to them differently?

How do I refer to an element in each of the arrays in the same way (?????? in my printMatrix function)?

int main()
{
   int **intMatrix1;
   int *intMatrix2;

   int i, j, lines, columns;

   lines = 3;
   columns = 2;

   /************************* intMatrix1 ****************************/

   intMatrix1 = (int **)malloc(lines * sizeof(int *));

   for (i = 0; i < lines; ++i)
      intMatrix1[i] = (int *)malloc(columns * sizeof(int));

   for (i = 0; i < lines; ++i)
   {
       for (j = 0; j < columns; ++j)
       {
       printf("Type a number for intMatrix1[%d][%d]\t", i, j);
       scanf("%d", &intMatrix1[i][j]); 
       }
   }

   /************************* intMatrix2 ****************************/ 

   intMatrix2 = (int *)malloc(lines * columns * sizeof(int));

   for (i = 0; i < lines; ++i)
   {
       for (j = 0; j < columns; ++j)
       {
       printf("Type a number for intMatrix2[%d][%d]\t", i, j);
       scanf("%d", &intMatrix2[(i * lines)+j]);
       }
   }

   /************** printing intMatrix1 & intMatrix2 ****************/

   printf("intMatrix1:\n\n");
   printMatrix(*intMatrix1, lines, columns);

   printf("intMatrix2:\n\n");
   printMatrix(intMatrix2, lines, columns);
}


/************************* printMatrix ****************************/

void printMatrix(int *ptArray, int h, int w)
{
    int i, j;

    printf("Printing matrix...\n\n\n");

    for (i = 0; i < h; ++i)
        for (j = 0; j < w; ++j)
        printf("array[%d][%d] ==============> %d\n, i, j, ??????);
}
like image 809
tempy Avatar asked Oct 08 '12 15:10

tempy


1 Answers

You are dereferencing the Matrix1 two times..

Matrix1[i][j] ;

It means that it is a 2D array or a double pointer declared like this.

int **Matrix1 ;

A double pointer could be thought of as array of pointers. Its each element is a pointer itself, so it is dereferenced once to reach at the pointer element, and dereferenced twice to access the data member of that member pointer or array. This statement as you you wrote is equivalent to this one..

Matrix1[i][j] ;   //is ~ to

*( *(Matrix1 + i) + j) ;

For a single pointer like this.

int *Matrix2 ;

You can derefernce it only once, like this.

Matrix2[i] ;  //is ~ to
*(Matrix2 + i) ;

This statement which you wrote..

Matrix2[(i * lines)+j] ;
         |-----------|

This portion evaluates to a single number, so it derefenced one time.

(i * lines) + j ;

As for your printmatrix() function, the ptArray passed to it is a single pointer. So you cannot dereference it twice.

Perhaps you can get better understanding of static and dynamic 2D arrays from my answer here.

2D-array as argument to function

like image 88
Coding Mash Avatar answered Oct 17 '22 21:10

Coding Mash