# Help understanding why this concept doesn't work

Contents
Intro
My code
My goal
Actual results

Intro
I have debugged my code to this point and figured out that what I am trying to do with this piece of code seems to be the culprit, I have greatly simplified the elements in the code so that it is easier to understand, but the results are the same.
I am trying to create a function that cycles through two rows of information. The first row cycles through the elements of an array (ROW1) using a global variable (R1). The second row cycles through the elements of another array (ROW2) using another global variable (R2), but the elements in Array ROW2 are themselves arrays holding the information for the second row, and being advanced by the first global variable R1. I know the code doesn’t work, although mathematically it should to my mind; I need help understanding why it doesn’t work and how I could possibly make it work.
I have a feeling it has something to do with pointers and arrays, and have referenced several books but I’m still not sure what it is I’m missing.

My Code
int R1 = 0; //Global variable #1
int R2 = 0; // Global variable #2
int X = {0, 1, 2}; //Interim array #1, holding Row2’s info
int Y = {3, 4, 5}; //Interim array #2, holding Row2’s info
int Z = {6, 7, 8}; //Interim array #3, holding Row2’s info
int ROW1 = {0, 1, 2}; //Array holding Row1’s info
int ROW2 = {X[R1], Y[R1], Z[R1]}; //Array pointing to the Arrays holding Row2’s info

void setup(){
Serial.begin(9600);
Serial.println(“Row1 Row2”);
for(R2 = 0; R2 <3; R2++){ // Advance Row2, 3 times
for(R1 = 0; R1 < 3; R1++){ // Advance Row1, 3 times
Serial.print(ROW1[R1]);
Serial.print(" ");
Serial.println(ROW2[R2]);
}
Serial.println();
}
}
void loop(){}

My Goal
To my mind, and I know this is wrong because it’s not doing what I want, the sketch:
enters the R2 for loop (R2 equals 0), enters the R1 for loop (R1 equals 0), prints out ROW1 (equal to 0), prints out ROW2 (equal to X[R1], equal to X, equal to 0);
advances the R1 for loop (R1 equals 1), prints out ROW1 (equal to 1), prints out ROW2 (equal to X[R1], equal to X, equal to 1);
and so on and so on until the serial monitor reads out like this:
Row1 Row2
0 0
1 1
2 2

0 3
1 4
2 5

0 6
1 7
2 8

Actual Results
The serial monitor is actually reading out like:
Row1 Row2
0 0
1 0
2 0

0 3
1 3
2 3

0 6
1 6
2 6

Get rid of XYZ and ROW1 and make ROW2 a two dimensional array with the stuff from XYZ.

ROW2 = {
{0,1,2},
{4,5,6},
{7,8,9}
};

HazardsMind:
Get rid of XYZ and ROW1 and make ROW2 a two dimensional array with the stuff from XYZ.

ROW2 = {
{0,1,2},
{4,5,6},
{7,8,9}
};

Thanks! I see how that works, and would produce the results that I posted are my end goal.
But your answer has made me realize that my goal is actually to be able to keep the XYZ arrays, because the names of the arrays are important to my sketch. Another way of putting it is: although the information in the XYZ arrays is the final product to be reported, the XYZ arrays themselves are information that will be input into the ROW2 array.
Is there any way of doing that?

There are two ways you can do that...

``````int X = {0, 1, 2}; //Interim array #1, holding Row2's info
int Y = {3, 4, 5};  //Interim array #2, holding Row2's info
int Z = {6, 7, 8}; //Interim array #3, holding Row2's info
int *ROW2 = {X,Y,Z};
``````

or...

``````int ROW2 = {
{0,1,2},
{4,5,6},
{7,8,9}
};

int *X = (int *)ROW2;
int *Y = (int *)(ROW2 + 3);
int *Z = (int *)(ROW2 + 6);
``````
``````int ROW2 = {X[R1], Y[R1], Z[R1]}; //Array pointing to the Arrays holding Row2's info
``````

Concept-wise, this line is your problem. I'm surprised that it even compiles, but if it does, it does NOT end up "pointing to arrays", it's just a new array that has (perhaps) the zero-eth element of each of the other arrays in it.

You COULD make it point to other arrays with statements something like:

``````int *ROW2 = {X, Y, Z};
:
Serial.println((ROW2[R2])[R1]);
``````

DavidOConnor:
There are two ways you can do that...

``````int X = {0, 1, 2}; //Interim array #1, holding Row2's info
``````

int Y = {3, 4, 5};  //Interim array #2, holding Row2's info
int Z = {6, 7, 8}; //Interim array #3, holding Row2's info
int *ROW2 = {X,Y,Z};

``````

or...

``````

int ROW2 = {
{0,1,2},
{4,5,6},
{7,8,9}
};

int *X = (int *)ROW2;
int *Y = (int *)(ROW2 + 3);
int *Z = (int *)(ROW2 + 6);

Thanks!
But how do I get the global variable R1 to decide the elements in the XYZ arrays. For example, when R2 is equal to 0, the ROW2 array should use the X array, but then global variable R1 should ensure that the elements in ROW1 and X always align. This seems to be where the code breaks down. It doesn't seem to like that I am asking it to first reference the ROW2 array using R2, and then go back and reference the X array using the R1.

westfw:

``````int ROW2 = {X[R1], Y[R1], Z[R1]}; //Array pointing to the Arrays holding Row2's info
``````

Concept-wise, this line is your problem. I'm surprised that it even compiles, but if it does, it does NOT end up "pointing to arrays", it's just a new array that has (perhaps) the zero-eth element of each of the other arrays in it.

You COULD make it point to other arrays with statements something like:

``````int *ROW2 = {X, Y, Z};
``````

:
Serial.println((ROW2[R2])[R1]);

Hey! Thanks! That worked perfectly! Exactly what I was trying to do!
Can I ask why I need the asterix in front of ROW2? It gave me an error: invalid conversion from 'int*' to 'int' when I tried to compile without it.

Can I ask why I need the asterix in front of ROW2? It gave me an error: invalid conversion from 'int*' to 'int' when I tried to compile without it.

Actually, a better question is:
Do you know of any books or reference material where that information is taught or could be found?
An explanation will always be appreciated :), but pointing me to a text book would be easier for you, and help me learn more in the long run.
Thanks!

Well, this is standard C "pointer" stuff; any book on C should contain the information, although I'm not sure which ones do a good job of explaining them at an "intermediate" level (basic level: character arrays (strings) and pointers to structures. arrays of pointers to arrays makes it intermediate.)

An array of ints in C is essentially identical to a pointer to an int.
"int *foo;" is a pointer to an int, so you can assign an array (of ints) to it:

``````int myarray;
int * foo = myarray;
``````

foo now "points" to the array, or the first element of the array (same thing!) You could do foo and it would be the same thing as myarray;
Then you can have an array of pointers "int *foo = {myarray, myarray2, myarray3};"
Now foo gets the pointer to myarray2, and (foo) gets the third element of myarray2...

Does that make any sense? Traditionally, pointers are one of the more difficult concepts for people to understand when they're first learning C. It helps a lot to have some background in an assembly language, where "addresses of things" have to be dealt with on a nearly constant basis.