egArray was not declared in this scope

I'm doing a problem to print the array again in reverse order, but it keeps showing that "egArray was not declared in this scope". Can anyone help me?

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  int egArray[10]={8,7,6,3,2,1,5,4,9,10};
  int num;
}

void loop() {
  // put your main code here, to run repeatedly:
  for ( int index=egArray.length - 1; index > =0 ; index--) {
     Serial.println(egArray[index]);
  }
}

Its true?

If you read the instructions on how to use the forum well,

" How to get the best out of this forum"
so you shouldn't post code image.
Post the complete code using the appropriate tags.

Sorry, I had no idea about this. Will change right away.

your array only exists inside the setup() function by its a local variable.
To use it in other functions it needs to be declared as global.
Study about variables.

Here is also giving compilation error:
index=egArray.length - 1
.lengh is used to "Strings" .
"length() - Arduino Reference"

I modified it to look like this:
sizeof(egArray) -1

As the array is of "int", and int in arduino occupies 2, the size of your array will be 20

try this code:

// int egArray[10] = {8,7,6,3,2,1,5,4.9,10};   //  <<<<<--------- . between 4 and 9 my fault
 int egArray[10] = {8,7,6,3,2,1,5,4,9,10};

void setup() {
  Serial.begin(115200);
  delay(100);
}

void loop() {
  //for(int index = sizeof(egArray)/2;  index >= 0; index--)
 for(int index = (sizeof egArray / sizeof (int)) -1; index >= 0; index--)
  {Serial.println(egArray[index]);
  }
}

1 Like

make this change

int egArray[10]={8,7,6,3,2,1,5,4,9,10};
  int num;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  for ( int index=egArray.length - 1; index > =0 ; index--) {
     Serial.println(egArray[index]);
  }
}

read about

1 Like

WARNING: 'sizeof' returns the size IN BYTES. Your 9-int array has a size of 18 (or 36 on a 32-bit processor). You want:
for(int index = (sizeof egArray / sizeof (int)) -1; index >= 0; index--)

WARNING: Your 'int' array contains a 4.9 which will be truncated to 4.

1 Like

Tks @johnwasser

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