+ 1

Solve the question

#include <stdio.h> #include <stdlib.h> int main() { int z; // what would be the value of z ,so that the output will '4' int *x; x = (int *)malloc(sizeof(int)); char *c; c = (char *)malloc(sizeof(char)); *x = z; c = x; c++; printf("%d\n", *c); return 0; } // a) 1234 , b)4132 , c)2431 , d)1243

14th Aug 2022, 6:29 AM
Arijit Saha
Arijit Saha - avatar
2 Answers
+ 1
I will assume a little endian system here, as the given values would not work for a big endian system. Initially c = x; sets `c` to the lowest memory address of the byte sequence of `x`, i.e. it refers to the least significant byte. The increment then advances the pointer to the second byte. Assuming a standard integer representation with 8 bit, for that byte alone to represent 4, only the third bit may be set (since 2^2 = 4). In the context of the whole integer, that would be the 11nth bit, which contributes as 2^10 = 1024 to its overall value. The least significant byte can represent anything between 0 and 255, so any answer in the range 1024 to 1279 would be correct. This applies to both 1234 and 1243.
14th Aug 2022, 8:36 AM
Shadow
Shadow - avatar
0
Shadow answer is right But can u please explain it clearly
14th Aug 2022, 8:41 AM
Arijit Saha
Arijit Saha - avatar