Strange zero variable problem

I have found something quite strange, at least to me, in some of my code.

I have a for loop:

for(int i = 0; i < count; i++)

and inside that I assign a variable:

int jump = nameLength + i + 8

where nameLength is set in setup() as a non-zero number;

This code fails at the variable declaration however as soon as I change it to this:

int jump = nameLength + (i+1) + 7

it works perfectly!

Any idea why this happens?

Andy

Works fine for me...

int nameLength;
int count;

void setup( void )
{
  nameLength = 13;
  count = 4;
}

void loop( void )
{
for(int i = 0; i < count; i++) 
int jump = nameLength + i + 8;
}

Could this be the problem...

int jump = nameLength + i + 8 ;

The semicolon was there, I must have missed it when I copied the code.

Theres actually around 10 more lines of code in the loop before the variable is assigned, and when I use i on it's own, the whole block fails to work:(

It's not really an inconvenience, just peculiar

Theres actually around 10 more lines of code in the loop before the variable is assigned, and when I use i on it's own, the whole block fails to work:(

So, show the whole block. There is clearly a problem with the code that needs to be addressed, not masked.

ExtEEPROM ee;
#define disk1 0x50
int slot = 0;
while(ee.readByte(disk1, slot) != 0)
  slot++;
char startArr[6] = "";
for(int i = 0; i < slot; i++)
{
  for(int t = 0; t < 7; t++)
    startArr[t] = (char)ee.readByte(disk1, i+t);
  if(strcmp(startArr, ":Start:") == 0)
  {
    Serial.print("---Start at ");Serial.print(i);Serial.println("---");
    int nameLength = (int)ee.readByte(disk1, i+7);
    char name[nameLength];
    ee.readCharArr(disk1, i+8, nameLength, name);
    Serial.print("Name ");Serial.println(name);
    int noteCount = ee.readByte(disk1, nameLength + (i+1) + 7); //<--where the problem occurs
    Serial.print("Note Count ");Serial.println(noteCount);
  }
}

I am reading data from an external EEPROM chip with a library I have made, that's what ExtEEPROM ee is

This code fails at the variable declaration however as soon as I change it

I meant to ask earlier what this means. "fails at the variable declaration"?

for(int t = 0; t < 7; t++)

This loop will execute when t is 0, 1, 2, 3, 4, 5, and 6. That's 7 values.

char startArr[6]

Oops. Not enough room (and no space for the trailing NULL).

if(strcmp(startArr, ":Start:")

The str functions operate on strings, not character arrays. The difference between a character array and a string is that a string is a character array THAT IS NULL TERMINATED. startArr is NOT NULL-terminated.

    char name[nameLength];

Again, no room for the NULL, which you don't add.

For some reason, the strcmp was working fine. And also before changing the char array sizes, but some other things, my initial problem has gone away. I was forgetting about this and modifying other code and then came back and tried the original variable declaration and it worked.

Thanks for your help :slight_smile: