#include <stdio.h> #include <stdlib.h> void pointers(int *x, int *y, int z, int*
ID: 3592125 • Letter: #
Question
#include <stdio.h>
#include <stdlib.h>
void pointers(int *x, int *y, int z, int* w) {
printf(" In pointers, x[2], *y, z, w: %d, %d, %d, %d", x[2], *y, z, *w);
x[2] = 731;
*y = 855;
y = x;
z = 922;
*w = *w + 1;
printf(" Leaving pointers, x[2], *y, z, w: %d, %d, %d, %d", x[2], *y, z, *w);
}
int main() {
int *a = (int *) malloc(3 * sizeof (int)); // build array of three ints
int b = 20;
int c = 30;
int *d = &c;
a[0] = 2021; a[1] = 1; a[2] = 2; // initialize array
*d = b;
printf(" main, a[2], b, c, *d are: %d, %d, %d, %d", a[2], b, c, *d);
pointers(a, &b, c, d);
printf(" main, a[2], b, c, *d are: %d, %d, %d, %d", a[2], b, c, *d);
printf(" "); }
-------------result------------
result
main, a[2], b, c, *d are: 2, 20, 20, 20
In pointers, x[2], *y, z, w: 2, 20, 20, 20
Leaving pointers, x[2], *y, z, w: 731, 2021, 922, 21
main, a[2], b, c, *d are: 731, 855, 21, 21
----
can anyone explain how this code works?
i have no idea why c is 20 in first line of result
and in the second line of result, why does it output the same value as first line even if i put the different variables?
also, how can i get a value of *w ? and why c became 21 in last line?
I really do not understand this entire code, please explain me how it works why the results would be like that.
Explanation / Answer
For the first output line:
we can see that inside the main function 'c' is initialized with value 30 and b with value 20, but in line 16 we create a pointer variable, 'd' which is pointing 'c'. And in line 18 we assign *d with value of b (20). Therefore the value at the address pointed by d which is c will be updated to 20.
Second liine:
when we pass the values of a, &b, c, d to the function 'pointers', these are taken stored in the variables present in the argument list of the function definition of 'pointers' in the same order.
Therefore, array 'a' is now pointed by pointer 'x' . &b(address of b) is pointed by pointer y, value of 'c' is stored in z, and value of d is stored in the pointer w.
So, when we print the values of these new variables, we actually get the same values as passed from the main() function.
For third line:
Before printing the values again inside the 'pointers' function, we make some updates in the values of the variables. Like, x[2] is assigned to 731, *y to 855, z to 922, and *w is incremented by 1. But we can see that y is again initialised to x in line 7. Since, x holds the base address of the array 'a', *y now holds the value at the base address of the array and that is 2021. And w stores the value of pointer d which inturn holds value 20(line 18). So, now the value at w's address is incremented by one and becomes 21.
for fourth line:
After returning to main() function, all the changes made inside the 'pointers' function will be reflected on the actual variables as well because, we passed all those variables as references, meaning we passed their addresses to the 'pointers' function and all the changes were made at the actual addresses only.
Therefore even the value of *d will be updated to 21 and since it was pointing to c's location, c will also be updated to 21.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.