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 */
}
}
Discussion:
28 comments Page 2 of 3.
Sujith said:
8 years ago
In unwinding phase, the last value of s becomes 5 then how it sends 120?
Please someone explain clearly.
Please someone explain clearly.
RATHOD said:
9 years ago
Please anyone explain it properly.
Supraja said:
9 years ago
What an excellent answer @Jas. Well said.
Zhattuchand Hapshete said:
9 years ago
You are a genius, your explanation is satisfied @Tomas.
Shivu said:
9 years ago
Thanks @R K.
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.
The answer will always be 0 you can copy this code in your compiler.
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;
R K said:
1 decade ago
*j = &s means store the s address.
*j = s means store the s value.
*j = s means store the s value.
Swathi said:
1 decade ago
Please explain difference between *j=&s, *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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers