The Gateway to Computer Science Excellence
+2 votes
302 views
void fun(int *p)
{
    int q = 10;
    p = &q; z
}    
int main()
{
    int r = 20;
    int *p = &r;
    fun(p);
    printf("%d", *p);
    return 0;
}
in Programming by Active (2.4k points)
edited by | 302 views
0
p is passed by value. So, original p remains intact, and keeps pointing to r.

4 Answers

+3 votes
Best answer
Ignoring the mysterious "z" in body of fun,

Output: 20

when we call fun from main, the address of r(say 1000) which is stored in p is copied to "p" in fun,

In fun, we are not changing the value at this address 1000, what we're doing in line p=&q is changing the address itself, i.e now p of fun holds another address(say 2000). so back in main, there are no changes, the p of main still holds the address 1000, which has value 20

Now to be more clear about when value changes and when it doesn't, try this: copy-paste this code and add line *p=30(or any random int value you can think of) in fun before p=&q line once, then move it below p=&q, observe the changes in output.
by Active (2.1k points)
selected by
0

Moreover fun(p) is called from main(), but the value of fun is not stored anywhere in main(). If the code will be like this , then output will change

void fun(int *p)
{
int q = 10;
p = &q; z
}    
int main()
{
int r = 20;
int *p = &r;
r=fun(p);
printf("%d", *p);
return 0;
}
+2
fun is not returning anything, its return type is void
0
yes, right. It is no returning
0
is it due to scope of variable?... since it is a call by reference hence actually address of r is being sent to fun p. and there p starts to point q.. since original p was sent then in main *p should print 10?
0
Address of r i.e 1000(say), is copied in main - P, then it is copied in fun-P, then fun-P (which is just a variable to hold an int address) changes it value to address of q(say 3000), now main-P is still pointing to 1000, if we had changed value at address 1000 then we might be seeing any change in O/P
0
It's the best answer with the best explanation.
0 votes

Output: 20

It is an example of call by reference method.

In which we pass an address of a variable in the function as a parameter and whatever values are manipulated within the function they don't have any impact on original values.

here, we pass the address of integer r and store it in the p (pointer to the integer) then passed the p in the function fun and then p values are manipulated in the function but as it is a call by reference (we just send the reference not the actual value of p)

when printf statement executes the value of p is taken from the main function.

Therefore the output is 20

by Junior (579 points)
0 votes
The output will be 20

Because in C,the scope of ay local variable or function parameter is only within that function,

Thus here,the vaue of *p will become 10 but only till it is in the function body,as soon as it comes back to the main(),the value of *p becomes 20
by (303 points)
0 votes
Here p in fun is is formal parameter so it's life time is lime time of the function one the function fun gets executed the value assigned to the formal parameter is lost. So, in the main function the value of *p (which is actually different to that of the corresponding formal parameter) remains 20. so, 20 is printed
by Junior (719 points)
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,834 questions
57,853 answers
199,514 comments
108,392 users