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 1 of 3.

Kareena said:   5 years ago
Let's do with value of i=3.
1) *j=3 , s=1
s=s* *j; ----> s=1*3 ------> s=3
*j=*j - 1; ------> *j=2

2) *j=2 , s=3 (because s is static)
s=s* *j; ----> s=3*2 ------> s=6
*j=*j - 1; ------> *j=1

3) *j=1 , s=6 (because s is static)
s=s* *j; ----> s=6*1 ------> s=6
*j=*j - 1; ------> *j=0.

4) *j=0 , s=6 (because s is static)
while(*j!=0) ----> false.
It will have no effect.

5) Control back to step (3)
Value of s will remain same because it is static ---> s=6
*j =s; -----> *j=6

6) Control back to step (2)
Value of s will remain same because it is static ---> s=6
*j =s; -----> *j=6


7) Control back to step (1)
Value of s will remain same because it is static ---> s=6
*j =s; -----> *j=6

8) Control back to main
As it is call be refrence, i=*j ------> i=6.
(5)

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.

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.

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.

Neha said:   7 years ago
But j is having the address of i.. so *j means value at address of i and i will store the value. i think it should be j=s.

Theettam said:   1 decade ago
@Udam: Same doubt here! Also, after each return from recursion, that value of s in the stack shall be copied to *j right?

Tomas said:   9 years ago
Everything's more or less good, except that the factorial will be stored in i. After function call j will be destroyed.
(1)

RamRatan kumar said:   1 decade ago
Why can I store the address of s in j? I also access the value of s through j because it store the address of s;

Sahil said:   1 decade ago
The answer won't be 120 since s is static.

The answer will always be 0 you can copy this code in your compiler.

Sujith said:   8 years ago
In unwinding phase, the last value of s becomes 5 then how it sends 120?

Please someone explain clearly.


Post your comments here:

Your comments will be displayed after verification.