1

Is it possible to declare an array consisting of other arrays (with variable sizes) consisting of structs in C++? It would be really nice if there was an easy and efficient way (using for) to iteration over all structs inside an element of the array.

The struct is defined like this:

struct Number
{
  int x;
  int y;
};

For example, the data is something like:

{
    { {0,0}, {0,1} },
    { {0,0}, {0,1}, {1,0}, {0,0} },
    { {0,0}, },
    { {0,0}, {4,0} }
}

I would like to use this for a self made clock consisting of an Arduino Uno, an Ethernet shield, an RTC and a LED array. The solution shouldn't use more memory than needed. That's why I don't use a two dimensional array.

4
  • 1
    If you can't use std::vector<std::vector<Number>>, you're probably left on providing your own implementation for it. Commented May 21, 2016 at 20:56
  • So you want C or C++? Your question is contradictory. On one side, your asking for C/C++ solution, and your question has c++ tag, on the other side your stating you are using gcc compiler. Commented May 21, 2016 at 21:00
  • The answer to your question is no, the things you are asking for are in the C++ Standard Library. Maybe you can find another library that implements something similar to a std::vector. Commented May 21, 2016 at 21:04
  • I rewrote the confusion lines, I really would like to learn C++ so that's why I'm using C++. I was trying a solution like this: Number N0[] = { {0,0}, {1,1}, {2,2}, {4,4} }; Number N1[] = { {6,0}, {1,3}, {1,2} }; Number N2[] = { {7,0}, {1,2}, {0,2}, {4,8}, {6,6} }; Number *Numbers[] = { N0, N1, N2 }; But then I don't know how to iterate through the elements of Numbers[0]; So Numbers[0][0]->x gives me 0,0 and Numbers[0][1]-x gives me 1. Commented May 21, 2016 at 21:11

3 Answers 3

3

You can use Standard C++ For Arduino. It implements a std::vector

With that, you can use a vector of vectors

struct Number
{
  int x;
  int y;
};

using MultiNum = std::vector<std::vector<Number>>;

However, it is of a worthy note that, Arduino's memory is really small, and you should really have upper bounds to your memory usage. A vector of a vector without smartly using reserve may waste some memory...

Another option is:

Number x[][4] =
    {
    { {0,0}, {0,1} },
    { {0,0}, {0,1}, {1,0}, {0,0} },
    { {0,0}, },
    { {0,0}, {4,0} }
    };

Of cause, that dictates the fixed memory consumed at compile time. (A 4x4 Matrix of Number).

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, Number* var[]; or Number** var;. Case closed :)

EDIT: oh, you swapped to C++... Than save yourself a headache and use the std::vector. Well... you can make your own Vector even in C, but no templates.

1 Comment

I ended up using your solution, thank you also to the others that responded. The vector implementation for Arduino from GitHub got me much weird data corruptions errors which I couldn't fix. This is also a bit faster and I think uses less memory (and especially program memory).
0

If your concern is memory, you can always declare Number **var and manually allocate and reallocate space when you need to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.