944,098 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 892
  • C RSS
Oct 24th, 2009
0

Problem in Pointers!!

Expand 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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
its.avinash is offline Offline
4 posts
since Oct 2009
Oct 24th, 2009
0
Re: Problem in Pointers!!
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.
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Oct 24th, 2009
0
Re: Problem in Pointers!!
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.
SVR
Reputation Points: 10
Solved Threads: 4
Light Poster
SVR is offline Offline
44 posts
since May 2008
Oct 25th, 2009
0
Re: Problem in Pointers!!
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, 28 views)
Reputation Points: 13
Solved Threads: 3
Junior Poster
Gaiety is offline Offline
115 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Turbo C
Next Thread in C Forum Timeline: Reading few string from a .txt file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC