开发者

factorial using recursion+pointers [closed]

开发者 https://www.devze.com 2023-02-03 12:14 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhet开发者_StackOverfloworical andcannot be reasonably answered in its current
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhet开发者_StackOverfloworical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

i am learning C programming, i was trying to write a recursive function by using this prototype:

void fact(int *n);

The parameter of this function should be passed by reference. Thanks for your help.


I don't feel to be helpful in giving a complete solution -- this is just to show there is an answer:

void fact(int *n)
{
    if (*n > 1)
    {
        int tmp = *n - 1;
        fact(&tmp);
        *n *= tmp;
    }
}

I would never write a factorial function this way.

0

精彩评论

暂无评论...
验证码 换一张
取 消