1 votes 1 votes #include<stdio.h> int main() { int a; char *x; x = (char *) &a; a = 512; x[0] = 1; x[1] = 2; printf("%dn",a); return 0; } Rudra Pratap asked Jul 19, 2018 Rudra Pratap 2.9k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
2 votes 2 votes output will be machine dependent . on little endian machine output will be 513 Anil Ji answered Jul 19, 2018 Anil Ji comment Share Follow See all 7 Comments See all 7 7 Comments reply Show 4 previous comments Anil Ji commented Jul 20, 2018 reply Follow Share @srestha yes according to little endian 513.If nothing is given we follow the little endian but output will be different in case of big endian. 0 votes 0 votes Jason commented Jul 20, 2018 reply Follow Share I have doubt:- Why this code is not showing segmentation fault. #include <stdio.h> int main() { printf("%d\n%d\n", sizeof(int), sizeof(char)); int a = 512; char *p; p = (char *)&a; p[0] = 1; p[1] = 0; p[2] = 0; p[3] = 0; printf("%d",a); p[4] = 110; p[5] = 10; printf("\n%d",a); return 0; } Since, int is 4 byte and char is 1 byte, according to me it should show segmentation fault at p[4] = 110 and also its below line since we are accessing the location which is not allowed to access. 0 votes 0 votes Shaik Masthan commented Jul 20, 2018 reply Follow Share C has no bound checking therefore if you have a[2], you can access a[20] or a[76] etc.... i am not sure on these points :- in C, there is nothing called " the location which is not allowed to access. " it will give segmentation fault, when you write in read-only location, but if you want read in read-only location, you can do that. 0 votes 0 votes Please log in or register to add a comment.