So according to many places found by Google including http://www.arduino.cc/en/Reference/Array
you can declare an array, which I have done just like the very first example in the link.
You can then assign a value to the array. Which I have done as the above link describes in the only method it shows.
My code
int myInt[5];
myInt[0] = 1;
myInt[1] = 2;
myInt[2] = 3;
myInt[3] = 4;
myInt[4] = 5;
void setup(){
}
void loop() {
}
The errors
Arduino: 1.6.4 (Windows 8.1), Board: "Arduino Uno"
testingVariPlacement:2: error: 'myInt' does not name a type
testingVariPlacement:3: error: 'myInt' does not name a type
testingVariPlacement:4: error: 'myInt' does not name a type
testingVariPlacement:5: error: 'myInt' does not name a type
testingVariPlacement:6: error: 'myInt' does not name a type
'myInt' does not name a type
Yes I know there are better ways to accomplish the same task. However the link I posted above and many many other programming sites all state that this method should work.
So why doesn't it?
Is it something like See Trap #30 on this page ?
Also note that Trap # 11 uses that same method to assign a value to an array I am trying to use. The error the example is for it to show over-shooting an array, not that the method itself is bad.
It compiles when you move these....
myInt[0] = 1;
myInt[1] = 2;
myInt[2] = 3;
myInt[3] = 4;
myInt[4] = 5;
.... into setup()
If you know the values that want in the array declare and initialise it at the same time.
int myInt[] = {1, 2, 3, 4, 5};
The compiler will work out how many entries there are in the array for you.
I realise that the values you gave may only be an example but if the values will never exceed 255 then this would be better better practice.
byte myBytes[] = {1, 2, 3, 4, 5};
If the values in the array will never change this is even better
const byte myBytes[] = {1, 2, 3, 4, 5};
Thank you for the replies.
Yes I will be using a different method of assignment eventually.
But all the examples state they method I tried would work. They(the examples) make no mention that the assignment of a value to an array in this method must occur inside a function. This seems like a key omission of information considering most code style guides indicate that it is a good practice to declare/assign global variables prior to setup().
And it just seems inconsistent to me as a beginning student of programming that
int x[] = {25, 300}; is fine to do before setup()
but
int x[2];
x[0] = 25;
x[1] = 300;
is not fine to do before setup(). Especially considering that according to the examples they do the same thing.
It's got nothing to do with arrays specifically, it's like that for all variables.
Above setup() you can give a variable a value iff that's the same statement where you define the variable.
So this is ok:
int x=5;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
This isn't ok:
int x;
x=5;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
The last one has to be like this:
int x;
void setup() {
// put your setup code here, to run once:
x=5;
}
void loop() {
// put your main code here, to run repeatedly:
}
Especially considering that according to the examples they do the same thing.
Can you please post a link to an example that illustrates this ?
They don't in fact do the same thing.
int x[] = {25, 300};
declares and initialises the variable.
int x[];
only declares the variable
Beware of doing this in setup()
int x[2];
x[0] = 25;
x[1] = 300;
Or you will end up with a variable that is only valid to use in setup()
That's because it's done line per line. And with int x[2]; you already declared the var (without assignment) so you can't do that again. It's declared and ready for use inside a function.
With int x[2] = { 25, 300}; you do everything at once. Declare the array and assign them a start value.
So yeay, it would have been nice to say something about this is a reference. But to be clear, outside a function you can only assign a value to a variable when declaring it. Inside a far you can assign a value when declaring it or after. So inside a function your code works (only has a scope inside that function).
void setup(){
int myInt[5];
myInt[0] = 1;
myInt[1] = 2;
myInt[2] = 3;
myInt[3] = 4;
myInt[4] = 5;
}
void loop() {
}
UKHeliBob:
int x[];
only declares the variable
That without the other two lines with it that I wrote will not work at all because size is not specified either directly inside the brackets or by the number of members inside braces.
So both need to be compared in entirely. As I compared them.
int x[] = {50,300}; //declares and initializes in one line
int x[2]; //declares
x[0] = 1; //initializes
x[1] =2; //initializes
Two methods to do the same thing, declare the array and assign values(initialize). Right?
JimboZA:
It's got nothing to do with arrays specifically, it's like that for all variables.
Ah, very good to know. Thank you.
Code: [Select]
int x[];
only declares the variable
That without the other two lines with it that I wrote will not work at all because size is not specified either directly inside the brackets or by the number of members inside braces.
You are right, of course, but I am sure that you understand the point that I was trying to make.
As to the examples, you said
But all the examples state they method I tried would work.
Sorry, but the examples you provided links to say no such thing. They provide examples but not of what you did, ie declare the array as global then assign values to the array outside of a function. True, they don't say that you can't do it but neither do they say you can and has been pointed out the same rules apply to all variable types.
int x[] = {50,300}; //declares and initializes in one line
int x[2]; //declares
x[0] = 1; //initializes
x[1] =2; //initializes
Change the way you think about these two different things. Think about it this way:
int x[] = {50,300}; //declares and initializes in one line
int x[2]; //declares and initializes with possibly unknown values.
x[0] = 1; //assigns new value
x[1] =2; //assigns new value
When a variable is declared it can be initialized with a value. In fact it will always have some value and it may not be what you expect. After a variable is declared it can be assigned a new value but that assignment has to occur in a valid code block.