小编Cha*_*pul的帖子

使用main()函数递归

我正在编写一个程序来使用main()函数的递归来计算阶乘.

/* Print factorial */
#include <stdio.h>
#include <stdlib.h>

static char **p;

int main(int argc, char **argv)
{
        int n, rv;
        if (argc < 2) {
                printf("Usage: a.out <value>\n");
                exit(-1);
        }
        n = atoi(argv[1]);
        if (!n) {
                rv = 0;
        } else {
                if (n == 1) {
                        rv = 1;
                } else {
                        n = n - 1;
                        **p = n;
                        main(2, p);
                }
        }
        printf("%d\n", rv);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序编译使用gcc但在执行时,我得到一个分段错误**p = n.有人可以帮我修改上面的程序以获得正确的结果.另外, …

c linux recursion segmentation-fault

0
推荐指数
1
解决办法
9101
查看次数

标签 统计

c ×1

linux ×1

recursion ×1

segmentation-fault ×1