Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strange Output

Reply
Thread Tools

Strange Output

 
 
KellyB
Guest
Posts: n/a
 
      08-21-2008
#include <stdio.h>
#include <limits.h>

void printbin(void* num) {
size_t i, j;
unsigned char *pc = (unsigned char* )&num;
pc += sizeof(num) - 1;

for (i = 0 ; i < sizeof(num) ; pc--, i++ ) {
for ( j = 0; j < CHAR_BIT ; j++ ) {
putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
}
putchar(' ');
}
return ;
}

int main(void) {

size_t i, j;
float obj = 1234.0;
float obj1 = 1234.0;
unsigned char *pc = (unsigned char* )&obj;
pc += sizeof(obj) - 1;

for (i = 0 ; i < sizeof(obj) ; pc--, i++ ) {
for( j = 0; j < CHAR_BIT ; j++ ) {
putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
}
putchar(' ');
}
puts("");

printbin(&obj1);
return 0;
}

This is the output :
01000100 10011010 01000000 00000000
00000000 00011101 11111110 11100100

Why is it so ? It seems very strange.Can anyone please help me
understand whats going on here?

PS: I am using a little-endian processor
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      08-21-2008
KellyB wrote:
[...]
> void printbin(void* num) {
> unsigned char *pc = (unsigned char* )&num;

^^
bogus &. num is a pointer. Remover the '&'
 
Reply With Quote
 
 
 
 
KellyB
Guest
Posts: n/a
 
      08-21-2008
pete wrote:
> KellyB wrote:
>> #include <stdio.h>
>> #include <limits.h>
>>
>> void printbin(void* num) {
>> size_t i, j;
>> unsigned char *pc = (unsigned char* )&num;
>> pc += sizeof(num) - 1;
>>
>> for (i = 0 ; i < sizeof(num) ; pc--, i++ ) {
>> for ( j = 0; j < CHAR_BIT ; j++ ) {
>> putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
>> }
>> putchar(' ');
>> }
>> return ;
>> }

>
> That has a major problem,
> which is related to the reply that you got from Martin Ambuhl.
>
> As it stands, there is no way to determine
> the size of the object that (num) points to.
>
> sizeof(num) equals sizeof(void *).
>
>
> You need:
>
> #include <stddef.h>
> void printbin(void* num, size_t size);
>


Aah ..Thanks Martin and Pete . Got it.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-21-2008
KellyB <(E-Mail Removed)> writes:

> #include <stdio.h>
> #include <limits.h>
>
> void printbin(void* num) {
> size_t i, j;
> unsigned char *pc = (unsigned char* )&num;


The & is wrong. You already have the address.

> pc += sizeof(num) - 1;


And the size will be wrong. The size of the print is not the size of
the object.

>
> for (i = 0 ; i < sizeof(num) ; pc--, i++ ) {
> for ( j = 0; j < CHAR_BIT ; j++ ) {
> putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
> }
> putchar(' ');
> }
> return ;
> }
>
> int main(void) {
>
> size_t i, j;
> float obj = 1234.0;
> float obj1 = 1234.0;
> unsigned char *pc = (unsigned char* )&obj;
> pc += sizeof(obj) - 1;


Both OK here, though.

> for (i = 0 ; i < sizeof(obj) ; pc--, i++ ) {


There is a subtlety about running pointer loops backwards in that you
can't even construct a pointer that points "before" the first byte of
the object (you don't access it, but even constructing it is
technically undefined behaviour). You won't, in practise, experience
a problem though.

> for( j = 0; j < CHAR_BIT ; j++ ) {
> putchar( *pc & ( 1 << (CHAR_BIT - j - 1) ) ? '1' : '0');
> }
> putchar(' ');
> }
> puts("");
>
> printbin(&obj1);
> return 0;
> }


--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange problem - page output contains output from another request Paul ASP .Net 1 04-10-2007 03:41 PM
AVG Email Scanner activating at strange times with strange IP addresses dennispublic@hotmail.com Computer Support 1 08-26-2006 04:27 AM
GZIP output and input strange behaviour Nikk & Jak Anderson Java 2 04-26-2004 08:11 PM
Question About Strange 'C' Code Syntax ( Well strange to me anyway ) Harvey Twyman C Programming 8 10-25-2003 05:54 AM
very strange gcc output Miroslaw Osys C Programming 4 09-07-2003 08:03 PM



Advertisments