1 1 vote #include <stdio.h> int main() { short int a[10]; int i=0; for(i=0;i<10;i++) a[i] = 300 + i; char *c = (char*)a; printf("%d\n", *(c+4)); int *n = (int*)a; printf("%d\n",*(n)); } output ? Programming in C programming-in-c output pointers + – dd 1.5k views answer comment Share Follow Print See 1 comment 1 1 comment reply Prashant. commented Oct 19, 2016 reply Follow flag it is 46 and 300 1 1 replyShare Please log in or register to add a comment.
Best answer 3 3 votes Initaily short int array will be filled like: short int take 16 bits to represent (assume) 0 1 2 3 4 5 6 7 8 9 300 301 302 303 304 305 306 307 308 309 NOw it is type casted in to character type .( take 8 bits) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 44 256 45 256 46 256 47 256 48 256 49 256 50 256 51 256 52 256 53 256 Here pointer c point in start *(c+4)= skip 4 element from start = 46 int *n = (int*)a; depend on size of int if it is 16 bits then 0 1 2 3 4 5 6 7 8 9 300 301 302 303 304 305 306 307 308 309 print n = 300 Prashant. answered Oct 19, 2016 • selected Oct 19, 2016 by dd Prashant. comment Share Follow See all 7 Comments 7 7 Comments reply dd commented Oct 19, 2016 reply Follow flag and sir for, 32 bit int last print = 19726636 0 0 replyShare Prashant. commented Oct 19, 2016 reply Follow flag yes .....merge 32 bits 301 +300 number representation. 0 0 replyShare ST commented Oct 20, 2016 reply Follow flag @Anirudh sir, wouldn't the values in columns 1,3,5 and so on in your second table be '1' instead of '256' ? Assuming little endian machine, the first 5 bytes would be stored in increasing memory addresses as : 00101100 ||| 00000001 ||| 00101101 ||| 00000001 ||| 00101110 |||...[and so on] The fifth byte is 00101110 which translates to 46, so it prints 46. Also, the answer would NOT be 46 if we considered execution in Big endian machine Please tell me if I am misinterpreting anything 0 0 replyShare Sandip Shaw commented Oct 20, 2016 reply Follow flag Can you tell me the source to study this topic? 0 0 replyShare Akriti sood commented Dec 23, 2016 reply Follow flag @anirudh..here in your second table..why is it that always 256 is the second column..i mean you have written 44 | 256 | 45 | 256............ why cant it be 256 |44 |256 |45 .......... also,with 8 bits ,we can max represnt 255..how have u shown 256?? please explain these points. 0 0 replyShare Akriti sood commented Dec 23, 2016 reply Follow flag @debashish,how is it 19726636 for last print if 32 bits for int are used.. i am gtetting this pattern-00000001001011000000000100101101 and this does not evaluate to your result 0 0 replyShare ST commented Dec 23, 2016 reply Follow flag @Akriti The bit pattern which you wrote: 00000001001011000000000100101101 will be correct answer for big endian machines 0 0 replyShare Please log in or register to add a comment.