int main()
{
int x[8];
for(int i =0; i<= 8; i++)
x[i] = i;
return 0;
}
Identify the error in the above code. When will the error be detected?
What I think: I think thee error is that in the for statement I starts at 0 which means that x[i] will equal 1,2,3,4,5,6,7 before it equals 8. None of those numbers were listed in the initial array. So I think the error will be detected once the compiler hits the for statement.
This^^ may be completely wrong. Just figured I would give it my best before I asked you guys. Thanks.
x[8] isn't declared.
If you declare x[8] it will declare 8 elements called:
x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7].
They are 8, but the number stops at 7, because you need to count the 0 too.
So the error is that '<='.
Change from for(int i =0; i<= 8; i++)
to for(int i =0; i< 8; i++)