Using C. Please fill out the code and do not make modifications to the functions
ID: 3912270 • Letter: U
Question
Using C. Please fill out the code and do not make modifications to the functions or headers :
#include
#include
int foo(int* a, int* b, int c){
/*Swap the addresses stored in the pointer variables a and b*/
/*Decrement the value of integer variable c*/
/*Return c*/
}
int main(){
/*Declare three integers x,y and z and initialize them randomly to values in [0,10] */
/*Print the values of x, y and z*/
/*Call foo() appropriately, passing x,y,z as parameters*/
/*Print the values of x, y and z*/
/*Print the value returned by foo*/
return 0;
}
Explanation / Answer
#include <stdio.h>
int foo(int* a, int* b, int c){
printf(" Inside foo, Address of a = %d Address of b = %d c = %d",a,b,c);
/*Swap the addresses stored in the pointer variables a and b*/
int *temp;
temp = a;
a = b;
b = temp;
printf(" Inside foo After Swapping addresses , Address of a = %d Address of b = %d c = %d",a,b,c);
/*Decrement the value of integer variable c*/
c--;
/*Return c*/
return c;
}
int main(){
/*Declare three integers x,y and z and initialize them randomly to values in [0,10] */
int x,y,z;
x = 5;
y = 7;
z = 4;
/*Print the values of x, y and z*/
printf("x = %d y = %d z = %d",x,y,z);
/*Call foo() appropriately, passing x,y,z as parameters*/
int result = foo(&x,&y,z);
/*Print the values of x, y and z*/
printf(" x = %d y = %d z = %d",x,y,z);
/*Print the value returned by foo*/
printf(" Value returned from foo : %d",result);
return 0;
}
Output:
x = 5 y = 7 z = 4
Inside foo, Address of a = 309520424 Address of b = 309520428 c = 4
Inside foo After Swapping addresses , Address of a = 309520428 Address of b = 309520424 c = 4
x = 5 y = 7 z = 4
Value returned from foo : 3
4 a) yes , the return value is different than the value of integer z. It will be the decremented value of z by 1.
4 b) No, the value of x and y will be same after the call to foo() because the scope of a and b is local inside the function foo() . Inside the function , the addresses are swapped but will not reflect outside the function .
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.