C Programming - Pointers - Discussion

Discussion Forum : Pointers - Point Out Correct Statements (Q.No. 8)
8.
In the following program add a statement in the function fact() such that the factorial gets stored in j.
#include<stdio.h>
void fact(int*);

int main()
{
    int i=5;
    fact(&i);
    printf("%d\n", i);
    return 0;
}
void fact(int *j)
{
    static int s=1;
    if(*j!=0)
    {
        s = s**j;
        *j = *j-1;
        fact(j);
        /* Add a statement here */
    }
}
j=s;
*j=s;
*j=&s;
&j=s;
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
28 comments Page 3 of 3.

Akshay k said:   1 decade ago
@Rahul - s**j means multiply s with *j. You can see this as s*(*j).

Akshay said:   1 decade ago
@Jigar is correct. Store result in address of i i.e. at *j.

Therefore *j=s;

Rahul said:   1 decade ago
What does s**j mean?

Jigar Patel said:   1 decade ago
Here fact(j) is a recursive function so when fact(j) is called last time i=0 .

Then function returns
s=120 (factorial value)

So it we have to store in j mean we need to us *j=s.

Sk rashid ali said:   1 decade ago
Recursive function store the value at the stack.

So when when the condition is satisfied and the values that are store are executed in reverse order. So whatever the value of s at last will have to be stored in *j and that becomes the first value to be displayed.

Gowthami said:   1 decade ago
Can you give a brief explanation for this question sir.

Arjun Prasad said:   1 decade ago
After all this recursive calls 's' will finally have the factorial of 5. So it must be copied to j location so that it can be printed in main function. Hence B is right answer.

Jas said:   1 decade ago
Since A, C, D are wrong ANS. Therefore B is correct.


Post your comments here:

Your comments will be displayed after verification.