Using a 2D array properly

I have a big program I'm slightly adapting from someone else, so I won't post it all here, but I can show what my issue is.

I initialize a 2D array like this:

double Steps[][5] =  {
                        {1,3.555,1,1,76},
                        {2,3.675,0,1,76},
                        {3,7,0,0,0},
                        {4,2.755,1,1,83},
                        {5,2.295,1,0,83},
                        {6,2.875,0,0,83},
                        {7,2.295,0,1,83},
                        {8,1.410,0,1,90},
                        {9,1.410,1,1,90}
                        };

If I then do this:

Serial.println(Steps[0,0]); 
Serial.println(Steps[0,1]); 
Serial.println(Steps[0,2]);
Serial.println(Steps[0,3]); 
Serial.println(Steps[0,4]);

it won't compile and gives the below error:

error: no matching function for call to 'println(double [5])'
Serial.println(Steps[0,0]);

If I do this:

Serial.println(*Steps[0,0]); 
Serial.println(*Steps[0,1]); 
Serial.println(*Steps[0,2]);
Serial.println(*Steps[0,3]); 
Serial.println(*Steps[0,4]);

It compiles and prints
1.0
2.0
3.0
4.0
5.0

Which I can only assume is related to the array position in some way.

I have read up on pointer, arrays, referencing and dereferencing, but I still don't follow why this does what it does.

Can someone point me in the right direction here?

Thanks

I believe 2D arrays are read as arrayName [0][0], not arranyName[0,0]

uuggg, thank you. I've been staring at it for a day ands somehow didn't see that!!

1 Like

To explain why this works in the second case, Steps[0,1] for example is seen by c++ as Steps[1], so println receives array of 5 doubles for which it has no overload. By dereferencing it *Steps[0,1] you get 1st element of Steps[1] which is 2 in this case, and since it is just a single double, println can print it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.