system
December 6, 2009, 5:53pm
1
Hi there !
I am new to Arduino programming.
I came across a small problem while trying to assign values to a multi dimensional array.
Definition looks like this.
int MyArray[11][3];
Now to my problem.
To save code I would like to do something like this
MyArray [1][0] = 10,20,30;
But in this case only the first value "10" is assigned to the array.
MyArray [1][1] and MyArray [1][2] is 0.
If I do like this it works
MyArray [1][0] = 10;
MyArray [1][1] = 20;
MyArray [1][2] = 30;
Is there a smarter syntax or is this the only way?
/E.T
system
December 6, 2009, 5:59pm
2
If you want to initialise the array when you declare it, yes, there is a smarter way:
int MyArray[11][3] =
{
{10, 20, 30},
{// more values here},
// and so on.
};
"MyArray [1][0] = 10,20,30;"
Won't work, because 'C' evaluates the statment left-to-right, so the 10 is assigned, 20 and 30 are evaluated to be true, but the result is discarded.
system
December 6, 2009, 6:28pm
3
Hello and thank you for answering.
Perhaps I was not clear enough.
I would like to update the array like in your example
during runtime but I can not make it work.
/E.T
system
December 6, 2009, 7:18pm
4
I can not make it work.
It won't work the way you wrote it first.
You have to write it out in full.
system
December 7, 2009, 1:57pm
5
You can only assign one element at a time.
myArray[1][0] = 10;
myArray[1][1] = 20;
myArray[1][2] = 30;
You could do these things in a loop, if the right-hand side can be calculated.
for (int i = 0; i < 3; i++)
myArray[1][i] = (i+1)*10;