Array Usage Question

I'm using IDE 2.3.7 (for Arduino UNO R4 WiFi), Starter Kit project #7. The suggested code has two sequential lines : (1) int buttons[6]; (2) int buttons[0] = 2;
I don't think the "int" is appropriate in the second (assignment) statement. Regardless, the debugger rejects it with the "int" as "conflicting declarations", as I would expect. But eliminating the "int" in statement (2) results in " 'buttons' does not name a type". What is going on here??

We are trying to see more of this in context.

Post entire sketches to get better answers.

Guess: those statement are not in any function.

a7

Correct...

The "second" button[0] = 2 assignment statement should be inside one of the functions.

int buttons[6];

void setup() {
  Serial.begin(115200);

  buttons[0] = 2;
  Serial.println(buttons[0]);
}

void loop() {}

Result:

2

Isn't correct anywhere. It looks kinda like a definition

int buttons[0] = {2};

but an array of zero elements can't have more than zero initial values.

int buttons[0] = {};

works, but raises questions bout what buttons could possibly be used for.

Which you can answer for yourselves. I read about it and rejected what I would otherwise have learned as unimportant enough to not waste any of the few functioning memory cells I possess. :expressionless:

a7

Correct, but we need to see an exact copy of the actual code, rather than what you recollect.

Did you try to compile the code? The compiler should have flagged an error.

I moved the "buttons[0] = 2;" assignment into the setup() function, and it compiled fine. Can someone tell me what the rules are for what statements can/must be inside or outside of functions. I don't see anything about that in the language reference.

It is not clear that there actually was a problem.

This "starter kit project 7" has been a problem for a long time. The original code is...

int buttons[6];
// set up an array with 6 integers

int buttons[0] = 2;
// give the first element of the array the value 2 note that it counts from zero

int notes[] = {262, 294, 330, 349};
//corresponds to the notes C, D, E and F

void setup() {
  Serial.begin(9600);
}

void loop () {
  int keyVal = analogRead(A0);
  Serial.println(keyVal);
  
  if (keyVal == 1023){
    tone(8, notes[0]);
  }
  else if (keyVal >= 990 && keyVal <= 1010) {
    tone(8, notes[1]);
  }
  else if(keyVal >= 505 && keyVal <= 515) {
    tone(8, notes[2]);
  }
  else if(keyVal >= 5 && keyVal <= 10) {
    tone(8, notes[3]);
  }
  else{
    noTone(8);
  }
}

The comment under the "line in question" states its purpose, but does not execute it correctly.

That's a nice way of saying it's simply wrong.

1 Like

You, indeed, are not incorrect.

Yeah; definitely wrong. Should/could be:

int buttons[6] = {2};  // array of 6 integers, the first of which is 2 (the rest default to 0)

(but I'm not sure that's good style.)

Clearly worrying to see that

In a published example…

Clearly not new

Right; so, what's the provenance of that Starter Kit's content, and is there any mechanism to have it updated? I'd suggest it's beyond embarrassing to someone, to have it linger for > 13 years.

Reading further on several threads relating to this Project 7, there's a lot of pfaffing around.
A quick web search turned up this, however, which shows none of the characteristics being complained about.

Guess it's not worth fussing about.

1 Like

buttons [0] = 2; is an instruction to be executed at run time. Instructions are speciffied within funcitons.

variables can be defined both globally outside of functions or locally, for use within the function.

initial values for variables can be statically defined, for example

byte pinButs [] = {  10, 11, 12, 13 };

this would be equivalent to

byte pinButs [4];

void setup () {
    pinButs [0] = 10;
    pinButs [1] = 11;
    pinButs [2] = 12;
    pinButs [4] = 13;
}

but uses code executed at run time to initialize values for the array

the executable file has 3 essential parts

  • text segment is the code

  • data sement simply sepcifies the amount of RAM that needs to be allocated for global and static vaiables. They are initialized to zero when the program is loaed. for example int array [200];

  • bss segment specifies both the amount of RAM along with values. for example, ` int array [3] = { 100, 29, 8 };

It is. Everything is. When it is correct at the source, it does not become an issue.

"Choose your battles." Choose every one.

1 Like