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

2
  • @SaiSreenivas the same error appears Commented Jul 23, 2020 at 11:52
  • char *** pointer_to_array_of_strings; Commented Jul 23, 2020 at 12:16

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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 ++;
    }
  }
}

2 Comments

thank you, it worked, could you say why my version (char * pMatrix = matrix;) works for 1D array, but stops working for 2d array?
They both have different types when it comes to 2d array.
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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.