top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C Program Problem: Type case int pointer with char pointer and then reprint?

+4 votes
696 views

What will be the output of this code?

 void main(){
       int i=320;
       char *ptr=(char *)&i;
       printf("%d",*ptr); 
    }
posted Apr 9, 2014 by Khusboo

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
surprising its giving 64, dont know why need to know the reason...:(

1 Answer

+3 votes
 
Best answer

In Binary 320 is

   00000000 00000000 00000001 01000000
   -- 4th-- -- 3rd-- --2nd--  --1st--

There are two types of machine Little Endian and Big Endian. In Little Endian When any data is stored in memory the lower byte is stored in the lower address of the memory i.e 320 would be stored as -

01000000 00000001 00000000 00000000
   1st     2nd      3rd      4th         

Vice-Versa for Big Endian.

Now when you type casted the integer to the char the charcter pointer is accessing the value of the 1st byte which is obviously 64

Also need to mention that character pointer accesses only one byte of the memory whose address it is currently holding. If u need Further clarification ...Comment.

answer Apr 16, 2014 by Prakash
Please elaborate
Similar Questions
+1 vote

What type of conversion is not accepted in C and why?
a) char to int
b) float to char pointer
c) int to char
d) double to char

+4 votes

I am wanting to extract variable names and their size (int, char and etc.) from a c file.
Is there any way to extract that type of information?

+2 votes

A char can not be a +ve or -ve. last bit is signed bit in accumulator.but not useful for CHAR Whats the purpose? of doing it.

+1 vote

int arr[ ] = { 1, 2 };
p = arr; /* p is pointing to arr */

How pointer p will behave ?

...