Cannot convert from char (*) [10] to char ** in return

Possible duplicate:
Declaring a function returning a 2D array to a header file?

I am trying to have a simple getter function for a 2D array, and I cannot imagine the correct syntax for sending it.

I currently have the following:

class Sample { public: char **get2D(); private: static const int x = 8; static const int y = 10; char two_d[x][y]; }; char** Sample::get2D() { return two_d; }; 
+4
source share
2 answers

An array of arrays is different from an array of pointers to arrays. In your case, you cannot return the correct type without the width of your array ( y ) being published in your public interface. Without this, the compiler does not know how wide each row of the returned array is.

You can try the following:

 class Sample { public: static const int x = 8; static const int y = 10; typedef char Row[y]; Row *get2D(); private: char two_d[x][y]; }; 
+6
source

It would be much better to do this:

 const char& operator()(int x1, int y1) { // Better to throw an out-of-bounds exception, but this is illustrative. assert (x1 < x); assert (y1 < y); return two_d[x][y]; }; 

This allows you secure read-only access to internal arrays (you can check!).

0
source

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


All Articles