Setting values of array of arrays

There's gotta be a way to do this.

I had arrays left1 right1 top1 and bottom1 i made them into an array of arrays by doing this (found in different thread):

int* box1 = {top1,left1,right1,bottom1};

So I wanted to do something that walked up the pixels of the box something like

int* rows[16];

rows[0] = { 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23 };

to me that makes sense. That's basically what the other thing is doing right? but this does not work.

error: expected primary-expression before '{' token

Does this make sense? Would really like to figure this out. Working with arrays w/ arduino is sort of annoying.

Thanks!

You’re getting confused with arrays of pointers and arrays, try this

int rows = {1, 2, 3, 4, 5}; //a normal array of ints
int* collectionOfArrays[3]; // an array of pointers to int, size 3
collectionOfArrays[0] = rows; //set the first pointer to point to our rows array

if this was being called out before void setup()
rows[0] = { 38, …
then it is missing datatype declaration.

If it was being called in void loop,
then rows[0] refers to the data at location 0, and you are trying to assign a whole bunch.

Thanks for responding!

CrossRoads:
if this was being called out before void setup()
rows[0] = { 38, …
then it is missing datatype declaration.

If it was being called in void loop,
then rows[0] refers to the data at location 0, and you are trying to assign a whole bunch.

I was doing it in setup. The arrays are declared globally, in setup I’m trying to set each of the values for rows. At location 0 in row I want an array.

I am probably messing up arrays of pointers/arrays. This is confusing concept to me. I would rather not have to make an array and then set it, I’d rather just hard code in the array. because what I really want to do is this:

for(int r = 1; r < 46;r++){
while(r < 15){
rows[r] = { left1[r], right1[r],left2[r],right2[r],left3[r],right3[r] } ;
}
}

to dynamically set the rows based on a bunch of arrays.

Edit: Crap this doesn’t work either:

int brow[6];

for(int r = 1; r < 46;r++){
while(r < 15){
brow = { left1[r], right1[r],left2[r],right2[r],left3[r],right3[r] };
rows[r] = brow;
}
}

Edit2: Oh i see in the one just above this, it won’t work either because brow will be changing and rows will be pointing to brow so it’ll just be the last value. damn.

Your definition (and not a declaration, by the way):

int *rows[16];

can be expanded using the Right-Left Rule to: "rows is an array of 16 pointer to int". That means, that each element in that array is supposed to be the memory address (lvalue) of an integer variable. If the 16 variables are the letters of the alphabet, then the initialization would have to be:

  int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p;
  int *rows[16] =  {&a,&b,&c,&d,&e,&f,&g,&h,&i,&j,&k,&l,&m,&n,&o,&p};

Just remember that pointer variables can only have two legal values: an lvalue (or memory address), or null. Anything else should throw an error.

Array of arrays, too complicated. I'd rather go with one big array; elements 0-127 represent one thing, 128-255 another, 256-whatever a third, for example. That's what you're ending up with, how you access it is all the pointers & stuff do.
I had an array of fonts in progmem. Every 5 bytes was a character. To pull it out of memory, I accessed element, element[x+1, element[x+2], element[x+3], element[x+4].
x was determined with a switch:case decoding of each character coming in over serial.

switch(incomingByte){
case 'a':
index = 5;
break;
case 'b':
index = 10;
break;
}

All comes down to how you visualize your data.
etc.

econjack:
Your definition (and not a declaration, by the way):

int *rows[16];

can be expanded using the Right-Left Rule to: "rows is an array of 16 pointer to int". That means, that each element in that array is supposed to be the memory address (lvalue) of an integer variable. If the 16 variables are the letters of the alphabet, then the initialization would have to be:

  int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p;

int *rows[16] =  {&a,&b,&c,&d,&e,&f,&g,&h,&i,&j,&k,&l,&m,&n,&o,&p};




Just remember that pointer variables can only have two legal values: an lvalue (or memory address), or null. Anything else should throw an error.

but what I want to do is this:

int[] *rows[46];

but that doesn't work. The point is I want to dynamically build arrays to put in rows with values from a series of other arrays and there doesn't seem to be a way to do that.

CrossRoads:
Array of arrays, too complicated. I'd rather go with one big array; elements 0-127 represent one thing, 128-255 another, 256-whatever a third, for example. That's what you're ending up with, how you access it is all the pointers & stuff do.
I had an array of fonts in progmem. Every 5 bytes was a character. To pull it out of memory, I accessed element, element[x+1, element[x+2], element[x+3], element[x+4].
x was determined with a switch:case decoding of each character coming in over serial.

switch(incomingByte){

case 'a':
index = 5;
break;
case 'b':
index = 10;
break;
}



All comes down to how you visualize your data.
etc.

So you're saying just make rows a huge array and know where the break points are for each one. That's annoying and almost not worth it. I might as well write out each row myself. No? There's no way to do this?

Well, I am not a real software guy, I'm a hardware designer, and I go with what makes sense and is code-able for me.
The real software guys may have better approaches.
Got to keep in mind that you only have so much memory too unless you are using a '1284P with 16K of SRAM.
Any array tho is really just a bunch of pre-saved bytes in memory, whether your code accesses it as
one long string of 96, or 8 groups of 16, or 24 groups of 4, or ....

You keep changing the specs: first it's 16, then 46. Sounds to me that you need to nail down exactly what you want to do. You said:

The point is I want to dynamically build arrays to put in rows with values from a series of other arrays and there doesn't seem to be a way to do that.

If you want to dynamically build arrays, you're going to need some kind of memory allocator (alloc, malloc, calloc), but dynamic memory allocation in an embedded system is rarely a good idea. Are you really saying that the values from a series of other arrays is unknown at compile time? If so, some form of dynamic (and risky) memory allocation is needed.

econjack:
You keep changing the specs: first it's 16, then 46. Sounds to me that you need to nail down exactly what you want to do. You said:

The point is I want to dynamically build arrays to put in rows with values from a series of other arrays and there doesn't seem to be a way to do that.

If you want to dynamically build arrays, you're going to need some kind of memory allocator (alloc, malloc, calloc), but dynamic memory allocation in an embedded system is rarely a good idea. Are you really saying that the values from a series of other arrays is unknown at compile time? If so, some form of dynamic (and risky) memory allocation is needed.

True. I said "something like" because I was simplifying for the thread. It doesn't matter if it's 16 or 46 or 126. All I wanted to know is if I could explicitly declare the array value of an array of arrays. If you want the specifics of the project I can post the whole code, but really all I needed was to be able to set the value of the array at one point (the row[0] = { something, something something }) from there I can take it.

It appears that the answer is "can't". and so I will have to manually construct all the arrays as well as conditionals to dictate how big they are since arduino doesn't support array length detection.

Yes the values are known at compile time so I could spend time to write them out, but that seems silly to me, but I suppose I have to rework what is silly when dealing with this level of code and I should just spend some hours manually writing them out. I am trying to write dynamic code that I don't have to change the values as much for different projects applications.

I think you should try using 2 Dimensional arrays. The syntax is simple

int array[3][2]=  {
    {1, 2};
    {3, 4},
    {5, 6}
};

You select a particular element by :

// will assign 3 to anElement
int anElement = array[1][0];

I believe he needs dynamically sized arrays added. It is possible to use my example to run through arrays, however you need to know the size of the initial array of pointers to other arrays. (There may be some flaws in my code I only compiled didn't run)

I think, based on what the OP describes, that he needs a form of vector or collection of arrays. Therefore the following should help -

Gammon Forum : Programming : STL : STL for microprocessors (taken from http://forum.arduino.cc/index.php/topic,175326.0.html)

Abhik:
I think you should try using 2 Dimensional arrays. The syntax is simple

int array[3][2]=  {

{1, 2};
    {3, 4},
    {5, 6}
};



You select a particular element by :


// will assign 3 to anElement
int anElement = array[1][0];

I don't think this will really help me because the second dimension would vary in size. Thanks for the post though.

liomry:
I believe he needs dynamically sized arrays added. It is possible to use my example to run through arrays, however you need to know the size of the initial array of pointers to other arrays. (There may be some flaws in my code I only compiled didn't run)

I think, based on what the OP describes, that he needs a form of vector or collection of arrays. Therefore the following should help -

Gammon Forum : Programming : STL : STL for microprocessors (taken from http://forum.arduino.cc/index.php/topic,175326.0.html)

Sweet! I hope that works. I will try it. I tried using the standardc++ library and had a lot of issues, but this might work. Thanks!

Still limited by the SRAM available, might as well define them as big as you can fit, determined by some trial and error testing, and go from there.