1

I'm trying to get an array of pointers to 2d arrays of booleans. How can this be achieved? This is for an Arduino (think they are a mix of C and C++?)

holmeswatson
  • 969
  • 3
  • 14
  • 39
  • Do you want the declaration syntax, or what? – Daniel Fischer Mar 07 '13 at 17:42
  • http://stackoverflow.com/questions/1768294/how-to-allocate-a-2d-array-of-pointers-in-c – noMAD Mar 07 '13 at 17:43
  • Are you looking for an array or dynamic allocation (i.e. an "array" [more or less] of a certain size known only at run-time)? This would be done differently in `C` than in `C++` for dynamic allocation. – RageD Mar 07 '13 at 17:44
  • http://stackoverflow.com/questions/1768294/how-to-allocate-a-2d-array-of-pointers-in-c – meyumer Mar 07 '13 at 17:47

3 Answers3

6

Based on your description, I think you're looking for something like:

bool (*arr[K])[M][N];

It breaks down as

       arr                -- arr
       arr[K]             -- is a K-element array
      *arr[K]             -- of pointers
     (*arr[K])[M]         -- to M-element arrays
     (*arr[K])[M][N]      -- of N-element arrays
bool (*arr[K])[M][N]      -- of bool
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • is it possible to declare it without having to declare M and N, i.e if you wanted to have 2d arrays of different sizes? – holmeswatson Mar 07 '13 at 18:02
  • No. The size of the array is part of the type; e.g., a 3x4 element array of `bool` is a different type from a 4x5 element array of `bool`, and a pointer to one isn't compatible with a pointer to the other. – John Bode Mar 07 '13 at 18:08
2

If you are using C++, and you don't want to input the size from the declaration, you can do that by allocating it dynamically.

int first_dim, second_dim;
// determine dimensions somewhere inside code

// create array of pointers to booleans
bool** arr[10];
for(i = 0; i < 10; i++){
    arr[i] = new bool*[first_dim];
    for(j = 0; j < first_dim; j++){
        arr[i][j] = new bool[second_dim];
    }
}

Make sure you delete all of your arrays when you are done using them.

NOTE

When you are trying to allocate 2d arrays, don't think of them as matrices or tables, each storing a boolean. For example, take an array of ints, an array declared as int arr[i][j], each element in the first "dimension" is of type int* and each element in the second "dimension" is of type int. So it is in fact an "array of arrays", if you will.

naxchange
  • 893
  • 1
  • 11
  • 24
  • (note to OP, this will be less efficient though, because the 2-d arrays will not be contiguous) – Stephen Lin Mar 07 '13 at 18:19
  • maybe, but how else would he do it, since he does not want `first_dim` and `second_dim` to be known from the start? (see comments on answer by @JohnBode above) – naxchange Mar 07 '13 at 18:21
  • he could allocate each `arr[n]` as an 1-d arrays instead of a 2-d array and manually index `arr[n][second_dim * i + j]` (that's what I would do, if performance mattered) – Stephen Lin Mar 07 '13 at 18:22
  • your last edit is somewhat misleading, btw, since it doesn't extend past 2d (with `bool arr[i][j][k]`, `bool[i]` is `bool (*)[k]`, not `bool**`...see [this](http://ideone.com/7SP9nu) – Stephen Lin Mar 07 '13 at 21:59
  • sure, I changed the type so he knows it's a separate example from the question he/she asked, but the point was to illustrate a better way to think about arrays. Thanks for the tip! – naxchange Mar 07 '13 at 22:06
  • 1
    I just mean that someone reading might assume that the first element of a 3-d array of `T` is convertible to `T**`, which is not the case; but anyway, they can read the comments so it's not a big deal. – Stephen Lin Mar 07 '13 at 22:08
  • Ah I see what you're saying. – naxchange Mar 07 '13 at 22:15
  • you can just link to the first answer [here](http://stackoverflow.com/questions/3920729/in-c-c-is-char-arrayname-a-pointer-to-a-pointer-to-a-pointer-or-a-pointe?rq=1), that's a good explanation. – Stephen Lin Mar 07 '13 at 22:16
  • (and technically with `bool arr[i][j]`, `arr[0]` is `bool [j]`. but it decays to `bool *`, if you want to be pedantic) – Stephen Lin Mar 07 '13 at 22:24
0

An array of pointers to 2D arrays of booleans looks like this (both in C and C++ - pick one, they're not the same language, nor a mixture):

 typedef bool TwoDBoolArr[10][10];
 typedef TwoDBoolArr *TwoDBoolArrPtr;
 typedef TwoDBoolArrPtr TwoDBoolArrPtrArray[10];

You need the last typedef, of course.

If you want less typedefs and decreased readability:

typedef bool (*TwoDBoolArrPtrArray[10])[10][10];