So hey all!
and CrossRoads, this stuff has been keeping me up late…
Thanks again for the all the feedback. It really got me kinda going a bit. I was sitting here reviewing and kept on seeing over and over, “If you can do it in C/C++, you can do it in Arduino”. I glance over at my book shelf and notice a dusty old copy of “The C Primer”! Classic!!!
So, I dust it off and give it an old peruse. Especially the sections on pointers. After all that, I don’t know if I understood better, or had just confused myself more.
Anyway, I was able to put together what I think is the start of a nice sample that really draws out what I was originally looking for. At this point I am well past needing what I originally wanted. I have a sketch I am working on that will use what I have learned. I’ll followup with that when its ready…
I did not include any of the memcopy approaches, yet, as I do not need it. See my TODO list in the sample header.
In the meantime, here is my preliminary sample, all dressed up. I still have some issues and still don’t understand what is going on in Part 2- approach 2. This should compile and run in setup().
And one other thing, I am with Archibald. And I am that kind of nerd. something from C# like IList, ICollection and IDictionary would be great, but I do understand the need to whittle Arduino development down to bare required basics. Those C# collection libraries can be large.
Also, C/C++ foundation of Arduino!!!??? Why did I get these errors when something crashed in the IDE:
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)…etc, etc, etc…
The IDE does look like Java?! Is it opensource as well? When I get my chops back I plan to have a look at the Arduino source, just to blow my mind.
If anyone is interested, review the sample and possibly offer insight into my outstanding issues. They are identified pretty clear in the code.
Here ya go, should just be able to copy, paste, compile and run. Enjoy!
//////////////////////////////////////////////
// "Almost All About Arrays, kinda..."
// compiled by SwartHack 5/19/2015
// contributers to my effort: All of Arduino Forum community and specifically:
// pYro_65, CrossRoads, MarkT, michinyon, UKHeliBob and Archibald
//
// TODO - convert to using byte instead of int.
// TODO - add a 3d example, try a 4d and blow your mind
// TODO - Nest, Nest, Nest where we can. Show examples of nesting and variable elimination.
// TODO - add memcopy examples, or what I know as pseudo cloning.
// TODO - Anything else?
void setup() {
Serial.begin(9600);
//use this to store/format messages
char msg[80];
/***************************************************************************
Part 1 - 2d array
****************************************************************************
start with a standard 2d array, also known as a matrix. Lets visualize it
this way using a table analogy, just like a spreadsheet.
So we will have a heirarchy of {table, row, column, cell}. Also frequently
used is the graphics display analogy of {screen, column, row and pixel}. Also serious bit heads
could understand the memory analogy, but that is beyond me
******************************************************************************/
sprintf(msg,"Part 1 - 2d array processing...\0");
Serial.println( msg );
int table[2][3] =
{
{ 11, 22, 33 },
{ 44, 55, 66 }
};
// or the same thing would also be
// int table[2][3] = {11, 22, 33, 44, 55, 66 };
// pointer to the first element of the array
// which is actually the cell matrix[0][0], or 11
int *row = table[0];
// print the value pointed to, and increment pointer
// to cycle through the entire array in order
for ( int i = 0; i < sizeof(table) / sizeof(int); i++ )
{
int cell = row[i]; //*row++; //row[0];
sprintf(msg,"value: %i",cell);
Serial.println( msg );
}
// now lets get a reference to the first row and print it
// this defines a pointer to the array of values in the row.
int * row1 = table[0] ;
int * row2 = table[1];
sprintf(msg, "\nvalues of row1: %i , %i, %i\0", row1[0],row1[1],row1[2]);
Serial.println (msg) ;
sprintf(msg, "values of row2: %i , %i, %i\n\0", row2[0],row2[1],row2[2]);
Serial.println (msg) ;
/* TODO use nested for loops to do the same thing TODO */
//////////////////////////////////////////////////////////////
// OK, this is fun!!!
///////////////////////////////////////////////////////////////
/*****************************************************************************
Part 2 - Array of Arrays
****************************************************************************
So this can be similar, if not the same as working with a 2d array. But there
can be significant differences as well. This can be illustrated using a
dataprocessing analogy like {data, record, element}. When we get data
we know we have records and elements, but we also know they may not be the
same size,let alone type. You can also nest up to the nth level dimension
(memory limited). For example, in a cell value you have an array of hex
values, etc. It tends to get mind bending a little and is a topic to
explore for a more advanced example. but for know just think Lawnmower Man.
For know lets keep it real with this example
*****************************************************************************
*/
/*
* Approach 1 - create an int array from two int arrays the end result being a [3][] array ????
*
* Issue 1 -- I have to declare it in peices!!??? why? See Approach 2
*
* Issue 2 -- The sizeof does not seem to be returning the desired result!!??
*
*/
sprintf(msg,"Part 2 - Array of Arrays, approach 1...\0");
Serial.println( msg );
int record_1[3] = {11, 22, 33};
int record_2[2] = { 44, 55 };
int *data[2] = { record_1, record_2 };
int * record1 = data[0];
sprintf(msg, "Record 1 has: %i elements(s)\0", sizeof(*record1) / sizeof(int));
Serial.println (msg) ;
sprintf(msg, "elements of record 1: %i , %i, %i\n\0", record1[0],record1[1],record1[2]);
Serial.println (msg) ;
int * record2 = data[1];
sprintf(msg, "Record 2 has: %i element(s)\0", sizeof(record2) / sizeof(int));
Serial.println (msg) ;
sprintf(msg,"elements of record 2: %i, %i\n\0", record2[0], record2[1]);
Serial.println (msg) ;
/* TODO - figure out sizeof so we can process in nested for loops - TODO */
///////////////
// want a unreported runtime error, try this
/////////////////
//sprintf(msg, "Value of record2: %i , %i, %i\n", record_2[0],record_2[1],record_2[2]);
//sprintf(msg, "Value of record2: %i", record2[0]);
/*
* Approach 2 - Initialize/define the array all in one go
*
* Issue -- 1. Seems you have to use () to define the inner integer array elements
* otherwise you get some scalar conversion error. Not quite sure on
* that one?!
*
* Issue -- 2. Does not seem to work as expected, but it is working. Is some operation
* happening here when using the ()
*/
sprintf(msg,"Part 2 - Array of Arrays, approach 2...\0");
Serial.println( msg );
int moredata[2] = { (11, 22, 33), (44, 55) };
int rec_1 = moredata[0];
int rec_2 = moredata[1];
sprintf(msg, "Rec 1 has: %i elements\0", sizeof(rec_1) / sizeof(int));
Serial.println (msg) ;
sprintf(msg, "elements of Rec 1: %i\0", rec_1); //returns 33
Serial.println (msg) ;
sprintf(msg, "Rec 2 has: %i elements\0", sizeof(rec_2) / sizeof(int));
Serial.println (msg) ;
sprintf(msg, "elements of Rec 2: %i\0", rec_2); //returns 55
Serial.println (msg) ;
Serial.end();
}
void loop() {
// put your main code here, to run repeatedly:
}