Problem in Pointers!!

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 1
Reputation: its.avinash is an unknown quantity at this point 
Solved Threads: 0
its.avinash its.avinash is offline Offline
Newbie Poster

Problem in Pointers!!

 
0
  #1
Oct 24th, 2009
Hi all!
Please somebody explain me how this snippet is working!!

  1. main()
  2. {
  3. float a 4.379;
  4. char*p;
  5. int i;
  6. p=(char*)&a;
  7. for(i=1;i<=3;i++)
  8. printf("%02X",(unsigned char)p[i]);
  9. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 431
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 52
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training
 
0
  #2
Oct 24th, 2009
Originally Posted by its.avinash View Post
Hi all!
Please somebody explain me how this snippet is working!!

  1. main()
  2. {
  3. float a 4.379;
  4. char*p;
  5. int i;
  6. p=(char*)&a;
  7. for(i=1;i<=3;i++)
  8. printf("%02X",(unsigned char)p[i]);
  9. }
The code simply assigns the address of "a" to the character pointer "p" and then proceeds to check the value of the four bytes at address p[0], p[1],p[2], and p[3]...
Last edited by gerard4143; Oct 24th, 2009 at 8:20 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
0
  #3
Oct 24th, 2009
Well it looks like that's what it's supposed to do.
It should print the hex representation of the floating point number 4.379.

But I don't see how it will compile and it will only print 3 of the 4 hex bytes.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 76
Reputation: Gaiety is an unknown quantity at this point 
Solved Threads: 2
Gaiety's Avatar
Gaiety Gaiety is offline Offline
Junior Poster in Training
 
0
  #4
Oct 25th, 2009
function to display the floating number
the float numbers are stored in sign exponent mantissa fashion
furthur reading
http://steve.hollasch.net/cgindex/coding/ieeefloat.html

void test()
{
float f=5.25;
char *p=(char *)&f;
int i;
for(i = 3; i >= 0; i-- )
printf("%x\n",p[i]);
}
refer the attachment for out put on my system .
Attached Files
File Type: zip New Microsoft Excel Worksheet.zip (50.1 KB, 5 views)
Minds are like parachutes - they only work when they are open
Gaiety
Reply With Quote Quick reply to this message  
Reply

Tags
char

Message:




Views: 371 | Replies: 3
Thread Tools Search this Thread



Tag cloud for char
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC