Help defining and assiging values to an array...

HI all,

So I'm trying to declare an array and assign values to it, and I can't figure out where my problem is.

Here's my code:

float wire_ohms[2][20];

wire_ohms[0][0] = 16; wire_ohms[1][0] = 5;
wire_ohms[0][1] = 18; wire_ohms[1][1] = 7;




void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

And here's the compiler's error message:

array_test:5: error: 'wire_ohms' does not name a type
 wire_ohms[0][1] = 18; wire_ohms[1][1] = 7;
                       ^
exit status 1

What am I doing wrong here? The error tells me the last assignment doesn't have a type, but isn't the type defined when defining the array? I've never had such a hard time defining an array in any other language I've used, so I feel like I'm asking a stupid question here.

Thanks for any help,
Randy

I am new to this.....and I have done assignment above 'setup' without compiler error and successful program execution (not saying that is proper, I may need to be educated LOL).

I think the compiler is telling you it wants to know what type your array variables are: i.e. byte, int, etc...

for instance: byte value[3] = {0, 0, 0);

hope this is what you need!

Dan

and I have done assignment above 'setup' without compiler error

No, you haven't.
You've defined and initialised variables, but you haven't done assignments outside of any function.

Thus, incorrect (assignment)

float wire_ohms[2][20];

wire_ohms[0][0] = 16; wire_ohms[1][0] = 5;
wire_ohms[0][1] = 18; wire_ohms[1][1] = 7;

and, correct (initialisation)

float wire_ohms[2][20] ={{16.0, 18.0}, {5.0, 7.0}};

(note the rest of the elements are filled with zeroes)

Well, this thread looks a lot different than it did a day ago..........

Delta_G:
Move those assignment lines into setup. You can't assign values like that at global scope. If you want to give initial values then you have to do it with an initializer list when you create the array.

Thanks Delta_G. Moving the assignment to setup() works, as in it compiles without errors.

But it's not really what I was looking for....

AWOL:
You've defined and initialised variables, but you haven't done assignments outside of any function.

So does the above statement mean that all assignments that happen after the array is defined must take place within a function? As in, any function, and not just in setup()?

I am only looking for answers and not to start the debate that has been removed from this thread. Really just looking to not have 20 assignment lines of code at the top of my program.

Thanks,
Randy

float array[2][2] = {{0,0}, {1,1}};

Is a global declaration/definition that creates a global named array and initializes it.

float array[2][2];

array[0][0] = 0.0;

Is a legal declaration/definition of the global array. The "initialization" is an illegal assignment that needs to be inside a function.

Your choice.

This may help you to see what they are saying:

float array[2][2] = {{0,0}, {1,1}}; // Valid syntax to initialize a global array

// array[0][0] = 0.0;                  // Illegal syntax if uncommented


void setup() {
  float localArray[2][2];           // array defined with local scope
  localArray[0][0] = 0.0;           // Legal syntax
  array[0][0] = 0.0;                // Legal syntax
}

void loop() {

}

You might also want to look up Scope under C++ Programming in the Useful Links at the top of this Forum.