For your consideration:
for (int i = 0; i < maxLength; i++); {
dest[i] = char(EEPROM.read(startAdr + i));
}
It took me a while to find the error, but why didn't the IDE see it as an error or at least a warning?
For your consideration:
for (int i = 0; i < maxLength; i++); {
dest[i] = char(EEPROM.read(startAdr + i));
}
It took me a while to find the error, but why didn't the IDE see it as an error or at least a warning?
Or the bracket at the end of the first line? Guessing that was a typo when posting and Delta_G is on the right track.
Edit: once again, not reading thoroughly bit me. Delta_G already mentioned the bracket.
I have a question about this.
Wouldn't the i inside the brackets be out of scope?
The i will only ever be a number between 0 and whatever maxlength is set to.
Delta_G:
Maybe there is another i.
That's a big existential question now, isn't?
There must be another 'i'. Global or local.
#include <EEPROM.h>
const int maxLength = 10;
char dest[maxLength];
int startAdr = 5;
void setup()
{
for (int i = 0; i < maxLength; i++)
;
{
dest[i] = char(EEPROM.read(startAdr + i));
}
}
void loop() {}
sketch_aug13a/sketch_aug13a.ino: In function 'void setup()':
sketch_aug13a:11:10: error: 'i' was not declared in this scope
dest[i] = char(EEPROM.read(startAdr + i));
^
exit status 1
'i' was not declared in this scope
Delta_G:
No he's right. It should be out of scope on that next line. It only exists in the for loop. But we can't see anymore of the code. Maybe there is another i.
Oops- bad example.
Yes, i was out of scope as I created the example. In the actual code I was not using the iterative inside the brackets.
Nevermind!
I've never ran into that particular out of scope issue. But I now know what to look for in the future. Still much to learn, me thinks.
That's what I gathered from the replies that followed. delta[] tries to take it outside the for loop. Which it can't.
It would be nice if the compiler threw a warning for an if(), while() or for() loop with a semicolon followed by a block, as that is almost always a mistake. But it doesn't.
DrAzzy:
It would be nice if the compiler threw a warning for an if(), while() or for() loop with a semicolon followed by a block, as that is almost always a mistake. But it doesn't.
GCC 7.3.0 does give you a warning:
test_for.cpp:9:4: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
for (int i = 0; i < maxLength; i++);
^~~
test_for.cpp:10:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
{
^
Ok Delta_G. I think, keyword think, I see what you're saying. The use of ; in that location pretty much terminates the for statement. Just like it terminates most other statements. Therefore, the bracketed i was not declared. Nor the one later on.
Shouldn't be allowed to use "i" as an array index anyway, since then when someone forgets code tags, it get italicised
dest = char(EEPROM.read(startAdr + i));
DangerToMyself:
The use of ; in that location pretty much terminates the for statement. Just like it terminates most other statements. Therefore, the bracketed i was not declared. Nor the one later on.
The 'if' statement can only contain one statement:
if ( <expression> ) <statement> [else <statement>]
Note the "else part is optional.
The ';' is a valid statement so it completes (not terminates) the 'if' statement. Another valid statement is a block statement:
{ <statement> <statement>... }
So you can write:
if ( <expression> )
{
<statement>
<statement>
}
[else
{
<statement>
<statement>
}]
In all cases, the "if ( )" is followed by a single "statement" which is optionally followed by an "else" and another single statement.