0

I want to make a pointer to an array of string(matrix[10][10]), but I get "initialization from incompatible pointer type" problem so how to fix it? And what is the problem?

void rotate(char matrix[10][10]){
  
  char * pMatrix = matrix;
  for(int j = 0; j < 10; j ++){
    for(int i = 9; i >= 0; i --){
      *pMatrix = matrix[i][j];
      pMatrix ++;
    }
  }

}

btw, my task is to perform 90 degree clockwise rotation of matrix, but my algorithm is terrible I know

3 Answers3

2

matrix is of type char (*)[10] (pointer to array of 10 char).

pMatrix is of type char * (pointer to char).

There is a type mismatch when you use:

char *pMatrix = matrix;

You try to assign a pointer to an array of 10 char to a pointer to char.

This is why you get the warning:

warning: initialization of 'char *' from incompatible pointer type 'char (*)[10]'


You need to dereference matrix

char *pMatrix = *matrix;

to get a pointer to char.

Could you say why my version (char * pMatrix = matrix;) works for 1D array, but stops working for 2d array?

matrix' type is different when declared as char matrix[10] and parameter of a function. Then matrix is actual equivalent to char *. The assignment from char * to char * is correct.

Take a look at:

Note that if you provide an amount of elements like in your case doesn't matter. char matrix[10] is equal to char matrix[] which is furthermore equal to char *matrix.


No guarantee if your algorithm works beside that. If you got problems with that, please ask a different question.

1

There are so many ways but I prefer this one:

void rotate(char matrix[10][10]){
  
  char *pMatrix = matrix[0];
  for(int j = 0; j < 10; j ++){
    for(int i = 9; i >= 0; i --){
      *pMatrix = matrix[i][j];
      pMatrix ++;
    }
  }
}

Or you could use this for better understanding

void rotate(char matrix[10][10]){
  
  char * pMatrix = &matrix[0][0];
  for(int j = 0; j < 10; j ++){
    for(int i = 9; i >= 0; i --){
      *pMatrix = matrix[i][j];
      pMatrix ++;
    }
  }
}
Sai Sreenivas
  • 1,690
  • 1
  • 7
  • 16
0

Check this,

void rotate(char matrix[][10]){
  char * pMatrix = &matrix[0][0];
  for(int j = 0; j < 10; j ++){
  for(int i = 9; i >= 0; i --){
      *pMatrix++ = matrix[i][j];
         }
      }
   }

enter image description here

One dimensional array:

Consider an array a of 4 integers as a[4].

Basic rule is both a and &a will point to same location.But they aren't pointing to the same type. (1) &a will point to the entire array which is an int[]. The pointer type is int(*)[]

(2) a, when it decays to a pointer, will point to the first element of the array which is an int. The pointer type is int * Now consider two-dimensional array.

Two dimensional array

Consider an array containing two 1D arrays, with each of them having two elements; a[2][2]. Since the number of dimensions increases, we have one more level of hierarchy i.e. &a, a and *a will point to the same location, but they aren't pointing to the same type.

(1) &a will point to the entire array which is int[][]. The pointer type is int(*)[][].

(2) a, when it decays to a pointer, will point to the first element of the 2D array which is an int[]. The pointer type is int(*)[]

(3) By using *a, we are de-referencing the pointer to a 1D array. Hence we will have a int * pointing to the first integer value of the 2D array.

Now considering your case,

(1) In your case is like the first case where you are just using 1 D array, and the one which I suggested as &matrix[0][0], the pointer points to the entire array. Hope this is clear now.

(2) Also, you need not pass the first dimension in the function.

Saurav Rai
  • 2,171
  • 1
  • 15
  • 29
  • thank you, it worked, could you say why my version (char * pMatrix = matrix;) works for 1D array, but stops working for 2d array? – Amir Amirov Jul 23 '20 at 11:58