a simple one....try this...

Go down

a simple one....try this...

Post  vijay on Mon Aug 18, 2008 8:00 am

consider this code....
Code:

  main()
 {
  char *p = "1111" ;
  printf("%d\n",*(int *)p) ;
 }
 

now given that ASCII value of '1' is p.... what does this output... answer in terms of p....
try this without actually running the code....

vijay

Posts : 6
Join date : 2008-08-18

View user profile

Back to top Go down

i think this is it......

Post  gajju on Sun Aug 31, 2008 7:59 am

"1111" is a string, not a decimal value 1111. You cannot convert it to an decimal simply by forcing a 'reinterpret' cast. All that will do is take the bit pattern used to represent the string and interpret it as an integer. The bit pattern for 1111 decimal is not the same as that for "1111" ASCII.

I am not sure what you mean by 'in terms of "p"', but u will get o/p 825307441 which is decimal equivalent of
ASCII value of string "1111"
avatar
gajju

Posts : 2
Join date : 2008-08-10

View user profile

Back to top Go down

Re: a simple one....try this...

Post  vijay on Tue Sep 02, 2008 3:26 pm

yeah..output is as what u said....and

825307441 = (256^3) * 49 + (256^2) * 49 + (256) * 49 + 49

"p" is ASCII value of '1' i.e = 49.... its not the character pointer "p" there... i forgot to mention that....

char *p = '1' '1' '1' '1'
so when u read a integer located at "p" as *(int *)p... u r going to read 4 bytes from "p" as an integer stored in binary... and thats
the output...hope i'm clear....

vijay

Posts : 6
Join date : 2008-08-18

View user profile

Back to top Go down

Re: a simple one....try this...

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum