4 votes 4 votes #include<stdio.h> int main() { int a = 12; void *ptr = (int *)&a; printf("%d", *ptr); getchar(); return 0; } A 12 B Compiler Error C Runt Time Error D 0 RISHI GUPTA 1 asked Apr 1, 2018 RISHI GUPTA 1 826 views answer comment Share Follow See 1 comment See all 1 1 comment reply siva140191 commented Apr 2, 2018 reply Follow Share Answer is B. I think it gives an Compilation Error.Coz of void is not reference to any address or any values.So We can use like this -->int *ptr = (int *)&a;...then it prints 12.And Compilations error is here-->printf("%d", *ptr);we printing values without typecasting from void to int. 0 votes 0 votes Please log in or register to add a comment.
3 votes 3 votes answer B you will get this error Compiler Error: 'void*' is not a pointer-to-object type because you have to typecast void pointer to int pointer. you have to use print("%d",*(int *)ptr); https://www.geeksforgeeks.org/void-pointer-c/ praveen sankar answered Apr 1, 2018 praveen sankar comment Share Follow See all 4 Comments See all 4 4 Comments reply srestha commented Apr 1, 2018 reply Follow Share plz explain this line void *ptr = (int *)&a; 0 votes 0 votes praveen sankar commented Apr 1, 2018 reply Follow Share address of a is assigned to or stored in ptr . type of a is given by (int *). casting address of a to int pointer is not necessary during initialization. but when you working with that pointer(during access,arithmetic) you have to type cast it to the desired type. 3 votes 3 votes srestha commented Apr 1, 2018 reply Follow Share ok address of a is typecasting to int pointer and then assign into void pointer and then we want to print value at void pointer right? 0 votes 0 votes praveen sankar commented Apr 2, 2018 reply Follow Share yeah only during the accessing the value of a pointer variable you need to use typecasting. Here typecasting tells compiler how many bytes it should get from memory.(int *) tells compiler to get 2 or 4 bytes depending on whether that compiler is 32 bytes or 64 bytes. 1 votes 1 votes Please log in or register to add a comment.
2 votes 2 votes Answer is compiler error Reason : printf("%d",*ptr), void * type cannot be de-referenced, we need do type casting before doing it. If you want 12, then printf("%d",*(int *)ptr) should be written. pankaj_vir answered Apr 1, 2018 pankaj_vir comment Share Follow See 1 comment See all 1 1 comment reply srestha commented Apr 1, 2018 reply Follow Share plz explain this line void *ptr = (int *)&a; 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes answer is B As *ptr is void pointer we have to type cast it before printing *ptr.Actually what is going on is ptr is pointing to address of a,now we are printing value to which ptr is pointing .When it goes to address of a then as ptr is void pointer it does not know until which byte it print i.e either 2B or 4B or 6B whatever.If we typecast it into int then it will print upto next 2B starting from address of a or if we typecast it into char then it will print next one byte from address of a. example let a=9 &a=1000 since int take 2B (16bit) a=(00000000)(00001001) first eight bit(MSB) store at 1001 location and next at 1000 location (This is called Little Endian) now if we typecast it into char i.e printf("%d",*(char *)ptr) then it will print only one bit from starting i.e 9 Govind Narayan Jha answered Apr 3, 2018 Govind Narayan Jha comment Share Follow See all 0 reply Please log in or register to add a comment.