40 40 votes What is printed by the following $\text{ANSI C}$ program? #include<stdio.h> int main(int argc, char *argv[]) { int x = 1, z[2] = {10, 11}; int *p = NULL; p = &x; *p = 10; p = &z[1]; *(&z[0] + 1) += 3; printf(“%d, %d, %d\n”, x, z[0], z[1]); return 0; } $1, 10, 11$ $1, 10, 14$ $10, 14, 11$ $10, 10, 14$ Programming in C gatecse-2022 programming programming-in-c pointers output one-mark + – Arjun 14.0k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
51 51 votes Option D is correct answer.Given that x=1, z[0]=10 and z[1]=11 int *p=NULL; p=&x; *p=10;By this code, they are accessing x and changing it’s value to 10. Therefore x=10 after the above code executed. *(&z[0]+1) += 3; As per precedence rule, z[0], &z[0], &z[0]+1, *(&z[0]+1) and *(&z[0]+1) += 3; evaluated in the order&z[0] --- address of z[0]&z[0] + 1 ---- address of z[1] $\text{ [ because z[0] and z[1] are contiguously allocated in the memory ]}$So, we can rewrite the above statement as *( address of z[1] ) += 3which is nothing but z[1] += 3 ==> z[1] = z[1] + 3Therefore z[1] = 14Notice that, z[0] not changed. Shaik Masthan answered Feb 15, 2022 • edited Sep 13 by Shaik Masthan Shaik Masthan comment Share Follow See all 2 Comments 2 2 Comments reply shihabkamel commented Mar 21, 2024 reply Follow flag Best 0 0 replyShare Saransh Maurya commented Jan 8, 2025 reply Follow flag well explained 0 0 replyShare Please log in or register to add a comment.
9 9 votes int x = 1, z[2] = {10, 11}; // Initially they are intialized one integer and one 1D array in the above statement// int *p = NULL; // here they initializing one integer pointer and assigning the address as null// p = &x; //here they are assigning address of x to the p // *p = 10; // here they are changing the value as 10 at the address of p is pointing // Through this statement x will become 10 p = &z[1]; // now they are assigning address of 2nd element to the pointer p,by dereferencing previous address// *(&z[0] + 1) += 3; // &z[0] means 1st element address // &z[0]+1 means next element of &z[0] i.e 2nd element in the array *(&z[0]+1) the value present at that address i.e 11(the value of second element in the array) by using assignment operator *(&z[0]+1)=*(&z[0]+1)+3 is equal 11+3=14(it is replaces the second element in the array as 14) printf(“%d, %d, %d\n”, x, z[0], z[1]); Answer: x=10,z[0]=10,z[1]=14. Yaswanth_1 answered Nov 9, 2023 Yaswanth_1 comment Share Follow 0 reply Please log in or register to add a comment.
6 6 votes the correct answer is option “D” now let me explain how? 4th line is normal, assigning the values to int x and an array “z” *p is a pointer and initially its value is null or 0. now in the 6th line, we store or assign the address of “x” into “p”. now in the pointer, we update the value from null or 0 to 10. since the pointer is pointing to “int x” therefore the value of int x will also change to 10. now we update the pointer to z[1] or {11}. now at last *(&z[0] + 1) += 3 ==> (&z[0]+1 means value at address z[0]+1, which is 11……. 11+3=14) here value of z[1] will update to 14 now printf(“%d, %d, %d\n”, x, z[0], z[1]) will print 10,10,14 imshivanshutiwari answered Nov 28, 2022 imshivanshutiwari comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote x = 1 , z[2] = { 10 , 11 } *p = Null ; p = &x; // which means p stores the x adress *p = 10; // which means x value 10 p = &z[1] // which means p stores the z[1] adress or it can also be written as p = z+1 or &z[1] both are the addresses Here , *( &z[0] + 1 ) += 3; // we know that the array is in consecutive address, so &z[0] + 1 = &z[1] *( &z[1] ) += 3; // given that p = &z[1] *(p) = *(p) + 3; *(p) = 11 + 3 = 14; so x = 10, z[0] = 10 not changed , z[1] = 14 Amjad. answered Feb 7, 2025 • edited Jul 2, 2025 by Amjad. Amjad. comment Share Follow 0 reply Please log in or register to add a comment.