求解,为什么后面输出的都是0?

求解,为什么后面输出的都是0?


#include<stdio.h>
int fibo(int n);

int main()
{
    int n = 1, result;
    while (n <= 30)
    {
        result = fibo(n);
        printf("%d\n", result);
        n++;
    }
    return 0;
}

int fibo(int n)
{
    int str[30] = {1,1};
    if (n == 1 || n == 2)
    {
        return str[n - 1];
    }
    else
    {
        str[n - 1] = str[n - 2] + str[n - 3];
        return str[n - 1];
    }
}

img

str数组是局部变量,作用域只在fibo函数内,函数返回后,生存期结束后被回收,可以尝试改为static int str[30] = {1,1};