C ++ dynamic array initialization with declaration

I have a function like this:

void findScarf1(bool ** matrix, int m, int n, int radius, int connectivity); 

and in main . I am creating a 2d dynamic array to pass in this function

  bool matrix[6][7] = { {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0} }; 

The problem is this:

 findScarf1(matrix, 6, 7, 3, 4); 

causes error C2664: 'findScarf1': cannot convert parameter 1 from 'bool [6] [7]' to 'bool **'

How to initialize an array compactly (simultaneously with a declaration)?

ps sorry if it duplicates the question, but I spent 1.5 hours on this.

+6
source share
4 answers

Technically, a 2D array is an array of 1D arrays. Therefore, it cannot convert to a pointer to a pointer. However, it can convert to a pointer to an array.

So this should work:

 void findScarf1(bool (*matrix)[7], int m, int n, int radius, int connectivity); 

Here bool (*matrix)[7] declares a pointer to an array of 7 bool.

Hope this helps.

+2
source

If you look at how an array is laid out in memory and compare it with how the β€œmatrix” of the pointer to the pointer is laid out, you will understand why you cannot pass the matrix as a pointer to a pointer.

Your matrix is ​​as follows:

  [matrix [0] [0] |  matrix [0] [1] |  ... |  matrix [0] [6] |  matrix [1] [0] |  matrix [1] [1] |  ...]

The matrix in the pointer to the pointer is as follows:

  [matrix [0] |  matrix [1] |  ...]
   |  |
   |  v
   |  [matrix [1] [0] |  matrix [1] [1] |  ...]
   v
   [matrix [0] [0] |  matrix [0] [1] |  ...]

You can solve this by changing the function argument:

 bool (*matrix)[7] 

This makes the matrix argument a pointer to an array that will work.


And by the way, the matrix variable you have is not dynamic, it is completely declared and initialized by the compiler, there is nothing dynamic there.

+3
source

You can define a function as:

 void findScarf1(bool * matrix, int m, int n, int radius, int connectivity); 

Or

 void findScarf1(bool matrix[][7], int m, int n, int radius, int connectivity); 

No matter how many dimensions an array has, it is just a block of linear memory.

When you use the first method, you may need to do a cast when calling this function:

 findScarf1((bool *)marix, 6, 7, 3, 4); 
+1
source

My following example may be useful to you:

 #include<stdio.h> void f(int (*m)[7]){ // As Nawaz answred printf("%d\n", m[3][3]); } void _f(int m[6][7]){ // As I commented to your question printf("%d\n", m[3][3]); } void _f_(int m[][7]){// Second form of Nawaz answe printf("%d\n", m[3][3]); } void f_(int (*m)[6][7]){// Pointer of 2D array printf("%d\n", (*m)[3][3]); } int main(){ int matrix[6][7] = { {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 3, 1, 0, 0}, {0, 0, 1, 4, 1, 0, 0}, {0, 0, 1, 5, 1, 0, 0}, {0, 0, 1, 6, 1, 0, 0}, {0, 0, 1, 7, 1, 0, 0} }; f(matrix); _f(matrix); _f_(matrix); f_(&matrix); return 1; } 

the question is not tied to c, but I compiled with gcc (I did not install g ++).

 ~$ gcc -Wall -pedantic 2d.c ~$ ./a.out 5 5 5 5 

I was not going to post the answer, but because I commented on the error in Nawaz, so during the experiment I wrote this code.

You can find it here, working in codepacde

+1
source

Source: https://habr.com/ru/post/949098/


All Articles