+ 4

when function foo(x,y) is called, will not then the value of n assigned to x and that of m to y? Help please!

#include<stdio.h> void foo(int n,int m) { n = 11; m = 93; //printf("%d %d\n",n,m); } int main() { int x =7, y =4; foo(x,y); printf("%d ",x); printf("%d",y); return 0; }

4th Jan 2019, 4:19 PM
Vijaya Mishra
Vijaya Mishra - avatar
4 odpowiedzi
+ 4
7 and 4 will be output Because here we are passing bu value So value of variable x and y will be passed which will be copied in n and m respectively Once v make change in n and m that won't affect x and y because x and y has different memory address
4th Jan 2019, 5:38 PM
Vijaya Mishra
Vijaya Mishra - avatar
+ 3
A hint. Add these lines... ...to foo(): printf("--> n:%x m:%x <--\n", &n, &m); ...to main(): printf("--> x:%x y:%x <--\n", &x, &y); Output: The addresses of each variable in memory.
4th Jan 2019, 4:38 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
...and they have different addresses because 'n' and 'm' are 'local' to foo. Space is allocated for these local variables on a temporary 'stack' when the function is called, and this memory is released when the function returns.
5th Jan 2019, 5:15 PM
Kirk Schafer
Kirk Schafer - avatar
0
4th Jan 2019, 7:59 PM
Mason Mixdorf