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++?)

4
  • Do you want the declaration syntax, or what? Commented Mar 7, 2013 at 17:42
  • stackoverflow.com/questions/1768294/… Commented Mar 7, 2013 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. Commented Mar 7, 2013 at 17:44
  • stackoverflow.com/questions/1768294/… Commented Mar 7, 2013 at 17:47

3 Answers 3

7

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
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.
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.

9 Comments

(note to OP, this will be less efficient though, because the 2-d arrays will not be contiguous)
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)
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)
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
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.
|
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];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.