Computer Club MNNIT
Would you like to react to this message? Create an account in a few clicks or log in to continue.

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

2 posters

Go down

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

Post  vijay 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

Back to top Go down

a simple one....try this... Empty i think this is it......

Post  gajju 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"
gajju
gajju

Posts : 2
Join date : 2008-08-10

Back to top Go down

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

Post  vijay 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

Back to top Go down

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

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


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