#include <stdio.h>
void fibonacci(void)
{
int i;
int fib[10];
for (i = 0; i < 10; i++)
{
if (i < 2)
fib[i] = 1;
else
fib[i] = fib[i-1] + fib[i-2];
}
printf("The 10-th Fibonacci number is %i .\n", fib[i]);
}
int main(void) {
fibonacci();
}In this example, the array fib is assigned
a size of 10. An array index for fib has allowed
values of [0,1,2,...,9]. The variable i has a value
10 when it comes out of the for-loop. Therefore,
when the printf statement attempts to access fib[10] through i,
the Out of bounds array index check produces
a red error.
The check also produces a red error if printf uses *(fib+i) instead
of fib[i].
Correction — Keep array index less than array sizeOne possible correction is to print fib[i-1] instead
of fib[i] after the for-loop.
#include <stdio.h>
void fibonacci(void)
{
int i;
int fib[10];
for (i = 0; i < 10; i++)
{
if (i < 2)
fib[i] = 1;
else
fib[i] = fib[i-1] + fib[i-2];
}
printf("The 10-th Fibonacci number is %i .\n", fib[i-1]);
}
int main(void) {
fibonacci();
}