The correct way to use an array to access multiple arrays of different lengths in C ++

I have several two-dimensional arrays of various lengths:

int Array_A[][2] = {{...}, {...}, {...}, ...};
int Array_B[][2] = {{...}, {...}, ...};
int Array_C[][2] = {{...}, ...};

I need another array that allows me to access these arrays:

??? Full_Array[] = {Array_A, Array_B, Array_C};

What is the correct type ???that I should use? I tried uint**and uint*but it does not work.

If this is not feasible, let us assume that I am not allowed to change the definition Array_A , Array_B... What is a good way to determine Full_Array?

+4
source share
3 answers

Array_A, Array_B Array_C - 2 int s, 2 int s.

, Full_Array 2 int s. :

int (*FullArray[])[2] = {Array_A, Array_B, Array_C};

, , , .

+7

Array_A, Array_B Array_C , & . , & , .

int Array_A[3][2] = { ... };
int Array_B[3][2] = { ... };
int Array_C[3][2] = { ... };

typedef int (*PtrType)[3][2];
PtrType Full_Array[] = {&Array_A, &Array_B, &Array_C};

.

int Array_A[][2] = { {}, {}, {} }; // Implied dimensions [3][2]
int Array_B[][2] = { {}, {} };     // Implied dimensions [2][2]
int Array_C[][2] = { {}, {}, {} }; // Implied dimensions [3][2]

typedef int (*PtrType)[3][2];
PtrType Full_Array[] = {&Array_A, &Array_B, &Array_C};
+1

decltype :

using PtrToArrayElem =
    decltype(&(Array_A[0]));                      // C++11, or
//  std::decay_t<decltype(Array_A)>;              // C++14 alternative, or
//  typename std::decay<decltype(Array_A)>::type; // C++11 version of above
PtrToArrayElem arrayOfPtrsToFirstElements[] = {Array_A, Array_B, Array_C};

, - , . , ( ) .


, , , , :

using PtrToArray = decltype(&Array_A);
PtrToArray arrayOfPtrToArray[] = {&Array_A, &Array_B, &Array_C};

. . , .

+1

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


All Articles