Identifying this logic error..

The code below does not print out the arrays correctly.

Does it have to do something with the allocation of these values?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

int arr1[10];
int arr2[10];
int arr3[10];
int i;

for (i = 0; i < 10; i++)
{
   arr1[i] = 100 + i;
   arr3[i] = 300 + i;
}

for (i = 0; i < 15; i++)
{
   arr2[i] = 200 + i;
}
You assign arr2 10 places but try and insert 15 items?
Right, but why does this error happen?
I dont get a compiler error when I run this.
I said Logic Error. Not a compiling error.

A logic error produces unintended or undesired output or other behaviour, although it may not immediately be recognised as such.
Last edited on
> why does this error happen?

Attempted out of range access to the array engenders undefined behaviour.

Undefined behaviour:
Anything at all can happen; the Standard imposes no requirements. The program may fail to compile, or it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended. - C FAQ
Topic archived. No new replies allowed.