Strange Output

Discussion in 'C Programming' started by KellyB, Aug 21, 2008.

  1. KellyB

    KellyB Guest

    #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
    KellyB, Aug 21, 2008
    #1
    1. Advertising

  2. KellyB wrote:
    [...]
    > void printbin(void* num) {
    > unsigned char *pc = (unsigned char* )&num;

    ^^
    bogus &. num is a pointer. Remover the '&'
    Martin Ambuhl, Aug 21, 2008
    #2
    1. Advertising

  3. KellyB

    KellyB Guest

    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.
    KellyB, Aug 21, 2008
    #3
  4. KellyB <> 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.
    Ben Bacarisse, Aug 21, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nikk & Jak Anderson

    GZIP output and input strange behaviour

    Nikk & Jak Anderson, Apr 26, 2004, in forum: Java
    Replies:
    2
    Views:
    1,127
    Roedy Green
    Apr 26, 2004
  2. Miroslaw Osys

    very strange gcc output

    Miroslaw Osys, Sep 7, 2003, in forum: C Programming
    Replies:
    4
    Views:
    453
    Emmanuel Delahaye
    Sep 7, 2003
  3. Harvey Twyman
    Replies:
    8
    Views:
    552
    August Derleth
    Oct 25, 2003
  4. Materialised

    Strange output problem

    Materialised, Apr 1, 2004, in forum: C Programming
    Replies:
    15
    Views:
    501
    Dan Pop
    Apr 6, 2004
  5. Paul
    Replies:
    1
    Views:
    424
Loading...

Share This Page