How to pass MyClass [] [] to MyClass **?

Why can not I convey

Point src[1][4] = { { Point(border,border), Point(border,h-border), Point(w-border,h-border), Point(w-border,h-border) } }; 

a

 polylines(frame, src, ns, 1, true, CV_RGB(255,255,255), 2); 

Where

polylines have a prototype

 void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 ) 

UPDATE

Ok, ok, I remembered this stuff :)

+4
source share
4 answers

Point[1][4] cannot be converted to Point** in this way.
Have a look at this question: Converting multidimensional arrays to pointers in C ++

The actual solution here is to use an STL container instead of a C-style array , for example. std::vector :

 typedef std::vector<Point> Shape; std::vector<Shape> myShapes; ... 

and pass it using a const reference instead of a const pointer:

 void polylines(Mat& img, const std::vector<Shape>& shapes, ...) 

Also note that src , ns , pts , etc. not very good names for your variables. Try choosing more meaningful names ... if not for your own sake, then do it for future readers of this code :)

+4
source

Because it is not the same thing. polylines asks for a pointer to pointers to Point , but you give it a pointer to an array [4] from Point .

Paraphrased: when you use src as an expression like this, it splits from an array into a pointer. In this case, it will decay from Point[1][4] to Point(*)[4] , that is, to a pointer to an array of size 4 of type Point . This is the location of the memory of 4 Point objects. polylines expects the memory location of a pointer to a Point object.

+2
source

Massive arrays and pointers to pointers do not imply the same memory layout. An array of arrays contains all its elements contiguously, while a pointer to pointers requires an array of contiguous pointers that point to (possibly non-contiguous) arrays of contiguous elements.

To do this conversion, you will need to select an array of pointers and point them to the correct entries in the array of arrays. In your case, since you have one set of values, it is even simpler:

 Point * pointer = &src[0][0]; int npts = 4; polylines(img, &pointer, &npts, 1, ...); 

Assuming ncontours is the number of pointers that the pointer points to, and npts is the number of points that individual pointers point to.

+1
source

Please check this question: casting char [] [] on char ** calls segfault?

Short answer: You should never do this.

0
source

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


All Articles