# Array of Arrays

I understand basic multidimensional arrays ... but I do something in Java quite a bit and I'm trying to figure out how to do the same thing in C++.

It's a simple concept really, I want to pass an array of parameters to a library.

For example:

``````int a1 = 3;
int a2 = 5;
int a3 = 10;

int b1 = 7;
int b2 = 2;
int b3 = 9;

int c1 = 8;
int c2 = 3;
int c3 = 5;

int parametersA[3] = {a1,a2,a3};
int parametersB[3] = {b1,b2,b3};
int parametersC[3] = {c1,c2,c3};
``````

AND THIS ... is what I would LIKE to do ... but I can't figure out how:

``````int parameters[3][3] = {parametersA, parametersB, parametersC};
``````

Can C++ do arrays of arrays?

Thank you,

Mike

You may have as many dimensions as you like.

``````int parameters[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
``````

But I'm trying to pass an array into an array, not manually define a 2D array ... I need to end up using loops to create these arrays on the fly.

You can fill it in on the fly

``````int parameters[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
...
...

parameters[0][1] = 9;
``````

If you already have three arrays, you can use an array of pointers

``````void setup()
{
Serial.begin(57600);

int parameterSet1[] = {1, 2, 3};
int parameterSet2[] = {4, 5, 6};
int parameterSet3[] = {7, 8, 9};

// array of pointers to parameter sets
int *parameters[] = {parameterSet1, parameterSet2, parameterSet3};

for (uint8_t paramCnt = 0; paramCnt < 3; paramCnt++)
{
for (uint8_t subCnt = 0; subCnt < 3; subCnt++)
{
Serial.println(parameters[paramCnt][subCnt]);
}
}
}
``````

Depending on what your parameters reflect, there might be better solutions.

Each array contains several values that will be passed into a library and then used for a method.

SO basically, I construct a bunch of these arrays which contain the parameters for a method in the library. Then I build those arrays into ONE array and toss it over to the library which then breaks out each array and then feeds one array at a time to the method which processes the initial values that I put into the base arrays.

I realize I could just pass one array at a time into the library, but I thought it would be cleaner to just pass it one object so I was trying to figure out how to do that ... in Java its a no brainer ... C++ tends to be quite rigid for my tastes ... but you just can't write code for a \$2 microcontroller in Java these days ... LOL

I tend to forget about the * and the & and those being the way things are tossed around in C ... thank you for showing me that example though I realize now how I need to be thinking to do this the way I want to.

:-)

If your parameter sets contain different information (e.g. an x, y and z position or start time and end time for something or ...), have you considered to use a struct or class for a parameter set? Use of a class would be the Java approach, I would think ;)

sterretje: If your parameter sets contain different information (e.g. an x, y and z position or start time and end time for something or ...), have you considered to use a struct or class for a parameter set? Use of a class would be the Java approach, I would think ;)

YES, yes it would ... but I've not delved into the deeper areas of C++ to that extent ... but I'm salivating at the thought ... do you have some example C code doing something like this?

... Then there's the cool thing in Java where pretty much anything can be put into an array or better yet ... a HashMap ... which has tremendous value in terms of convenience.

I picked up a Teensy 4.0 the other day and read somewhere that it can be programmed with some very streamlined Java engines that are out there for microcontroller programming. I realize that it's a much more advanced chip than your standard Arduino ... which makes them a lot more expensive to work with, but this one was \$20 which isn't too bad ... but I can't wait to dig into it and see what the Java world looks like in microcontrollers...

EasyGoing1: ... but I can't wait to dig into it and see what the Java world looks like in microcontrollers...

There's a reason microcontrollers are programmed in C / C++. Most people who use them need low-level hardware control and bit twiddling. These languages can get you down to that level and produce very efficient code -- undoubtedly more efficient than you could do yourself in assembly language. If you want to run Java, why not do so on a PC?

You cannot easily do this using C-style arrays, at least not in the same way you’d do it in Java.

``````[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]Arduino_Helpers[/color][/b][color=#434f54].[/color][color=#000000]h[/color][color=#434f54]>[/color] [color=#434f54]// [/color][u][color=#434f54]https://github.com/tttapa/Arduino-Helpers[/color][/u]
[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]AH[/color][/b][color=#434f54]/[/color][color=#000000]STL[/color][color=#434f54]/[/color][color=#00979c]array[/color][color=#434f54]>[/color]
[color=#434f54]// if available, use:[/color]
[color=#434f54]// #include <array>[/color]

[color=#5e6d03]using[/color] [color=#000000]std[/color][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]array[/color][color=#000000];[/color]

[color=#00979c]int[/color] [color=#000000]a1[/color] [color=#434f54]=[/color] [color=#000000]3[/color][color=#434f54],[/color] [color=#000000]a2[/color] [color=#434f54]=[/color] [color=#000000]5[/color][color=#434f54],[/color] [color=#000000]a3[/color] [color=#434f54]=[/color] [color=#000000]10[/color][color=#000000];[/color]
[color=#00979c]int[/color] [color=#000000]b1[/color] [color=#434f54]=[/color] [color=#000000]7[/color][color=#434f54],[/color] [color=#000000]b2[/color] [color=#434f54]=[/color] [color=#000000]2[/color][color=#434f54],[/color] [color=#000000]b3[/color] [color=#434f54]=[/color] [color=#000000]9[/color][color=#000000];[/color]
[color=#00979c]int[/color] [color=#000000]c1[/color] [color=#434f54]=[/color] [color=#000000]8[/color][color=#434f54],[/color] [color=#000000]c2[/color] [color=#434f54]=[/color] [color=#000000]3[/color][color=#434f54],[/color] [color=#000000]c3[/color] [color=#434f54]=[/color] [color=#000000]5[/color][color=#000000];[/color]

[color=#00979c]array[/color][color=#434f54]<[/color][color=#00979c]int[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54]>[/color] [color=#000000]parametersA[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]a1[/color][color=#434f54],[/color] [color=#000000]a2[/color][color=#434f54],[/color] [color=#000000]a3[/color][color=#000000]}[/color][color=#000000];[/color]
[color=#00979c]array[/color][color=#434f54]<[/color][color=#00979c]int[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54]>[/color] [color=#000000]parametersB[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]b1[/color][color=#434f54],[/color] [color=#000000]b2[/color][color=#434f54],[/color] [color=#000000]b3[/color][color=#000000]}[/color][color=#000000];[/color]
[color=#00979c]array[/color][color=#434f54]<[/color][color=#00979c]int[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54]>[/color] [color=#000000]parametersC[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]c1[/color][color=#434f54],[/color] [color=#000000]c2[/color][color=#434f54],[/color] [color=#000000]c3[/color][color=#000000]}[/color][color=#000000];[/color]

[color=#00979c]array[/color][color=#434f54]<[/color][color=#00979c]array[/color][color=#434f54]<[/color][color=#00979c]int[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54]>[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54]>[/color] [color=#000000]parameters[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]parametersA[/color][color=#434f54],[/color] [color=#000000]parametersB[/color][color=#434f54],[/color] [color=#000000]parametersC[/color][color=#000000]}[/color][color=#000000];[/color]

[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000]115200[/color][color=#000000])[/color][color=#000000];[/color]
[color=#5e6d03]for[/color] [color=#000000]([/color][color=#00979c]auto[/color] [color=#434f54]&[/color][color=#000000]param[/color] [color=#434f54]:[/color] [color=#000000]parameters[/color][color=#000000])[/color] [color=#000000]{[/color]
[color=#5e6d03]for[/color] [color=#000000]([/color][color=#00979c]auto[/color] [color=#434f54]&[/color][color=#000000]value[/color] [color=#434f54]:[/color] [color=#000000]param[/color][color=#000000])[/color] [color=#000000]{[/color]
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#000000]value[/color][color=#000000])[/color][color=#000000];[/color]
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]print[/color][color=#000000]([/color][color=#00979c]'\t'[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]
[b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]
[color=#000000]}[/color]

[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color][color=#000000]}[/color]
``````

If you’re on AVR, the header is not available as part of the toolchain, but you can use the Arduino Helpers library or ArduinoSTL to provide (parts of) the standard library.

But as others have mentioned, in your case, passing parameters as arrays doesn’t sound like very good design. Use a struct instead.

Pieter

that it can be programmed with some very streamlined Java engines

I hope you have full control over the garbage collector in these. If not, you can say bye bye to realtime ;)

gfvalvo: If you want to run Java, why not do so on a PC?

I'd love to ... just toss me a link to a PC that's the size of two quarters and costs around \$5 ... and I'm all over it.

;-)

I've been looking at Java Embedded and apparently they have it nailed down to were code running java on microcontrollers is just as efficient and capable as C ... but the processors need more memory than the typical Arduino has, as you might have predicted ...

Also, no luck in figuring out how to program a teensy 4.0 with Java ... can't seem to find that knowledge anywhere.

Mike

PieterP: But as others have mentioned, in your case, passing parameters as arrays doesn't sound like very good design. Use a struct instead.

Pieter

I've never created struct's before ... I'll look into it ... the code you posted looks interesting ... but it only amplifies to me, the rigid and limited nature of C ... why does C need to be so ... 'rocky' anyways? And it reeks of 1990's coding abilities ... like it's not been updated in decades... Is there a reason for that?

sterretje: I hope you have full control over the garbage collector in these. If not, you can say bye bye to realtime ;)

Garbage collection is one of Java's main selling points ... it handles it for you and it does it quite well too ...

EasyGoing1: Is there a reason for that?

Mostly to give Java programmers something to whine about :grin:

gfvalvo: Mostly to give Java programmers something to whine about :grin:

Well then ... MISSION ACCOMPLISHED! :D

EasyGoing1: Garbage collection is one of Java's main selling points ... it handles it for you and it does it quite well too ...

I'm fully aware of that. However, it possibly collects the garbage at times that you need every possible CPU cycle for something time critical. Unless you have full control and it actually listens immediately when you say "collect now", you have a problem.

I've only played a little bit with Java on a PC and never for time critical stuff, so I do not know how much control you have over the garbage collector. And I certainly don't know anything about embedded Java; it might immediately listen when you tell it to collect the garbage.

sterretje: I'm fully aware of that. However, it possibly collects the garbage at times that you need every possible CPU cycle for something time critical. Unless you have full control and it actually listens immediately when you say "collect now", you have a problem.

I've only played a little bit with Java on a PC and never for time critical stuff, so I do not know how much control you have over the garbage collector. And I certainly don't know anything about embedded Java; it might immediately listen when you tell it to collect the garbage.

I would think that any language worthy of the 'embedded' category is going to offer the developer absolute control over their world... Garbage collection in normal desktop Java happens automagically and only when it makes sense to do it ... what that means in practice I have no idea, but you can code java down into fractions of microseconds if that means anything ... I picked up a book on Swift this morning and started going through it ... until my eyeballs started phasing out into parallel universes ... I think Swift might be worse than C when it comes to datatype implementation ... and for a language that's supposedly more natural to read ... it leaves much to be desired in that area especially with object comparison notation ... lots of options ... and too many of them look the same ...

EasyGoing1:
I’ve never created struct’s before … I’ll look into it … the code you posted looks interesting … but it only amplifies to me, the rigid and limited nature of C … why does C need to be so … ‘rocky’ anyways? And it reeks of 1990’s coding abilities … like it’s not been updated in decades… Is there a reason for that?

The code I posted is C++, not C.

What do you mean by “rigid”? A rigid syntax avoids ambiguities, and a rigid type system is a good thing as well. C++'s type system is much more rigid than C’s. Rust, the most loved language has an even stricter type system and compiler than C++. Even dynamic languages like Python and JavaScript are evolving a more explicit type system, using type hints and TypeScript.

I don’t believe the code I posted is any more rigid than the equivalent Java code. The only difference is that the sizes of the arrays are fixed, which is absolutely a good thing on a microcontroller with 2 KiB of RAM, and not a limitation of the language.
You can of course use dynamic arrays, C++'s “vector<>” is just like Java “ArrayList<>”, but using dynamic memory on devices with little RAM comes with huge caveats.

I have no idea what you mean by “rocky”, but there’s nothing old-fashioned about declaring arrays. How would you do it more “2020-style”?

C++ is updated almost every three years. It’s backwards compatible with older versions, but it’s a modern language.
The latest version is C++20 and will be officially released this year. The compilers that ship with the Arduino IDE right now support up to C++17, but most Arduino boards use C++11 for some reason.

EasyGoing1:
I think Swift might be worse than C when it comes to datatype implementation …

What do you mean by “datatype implementation”? I’m afraid you have some unrealistic expectations of how programming languages should be designed.

EasyGoing1:
it leaves much to be desired in that area especially with object comparison notation … lots of options … and too many of them look the same …

What do you mean by “object comparison notation”? What kind of options?

PieterP:
I have no idea what you mean by “rocky”, but there’s nothing old-fashioned about declaring arrays. How would you do it more “2020-style”?

List myList = new List<>();