0

Can anyone explain the behavior of the code snippet?

#include <stdio.h> int main() { int a[] = {1, 2}; char *p; p = (char*)a; printf ("%d\n",*p); p = p + 1; printf ("%d",*p); return 0; }

19th Aug 2019, 1:53 PM
Misha
3 Réponses
+ 3
int require 4 bytes so a is 8 bytes in size. char requires 1 byte so you print 2 bytes of a. Change p to: int *p; and you would get both integers.
19th Aug 2019, 2:50 PM
John Wells
John Wells - avatar
+ 1
Thanks John Wells.
19th Aug 2019, 4:26 PM
Misha
0
John Wells can you please explain me about the code working
19th Aug 2019, 2:42 PM
Misha