representation of integers(again) very annoying..

Discussion in 'C Programming' started by Mantorok Redgormor, Oct 13, 2003.

  1. I have a member of a struct which is:
    int32_t ut_addr_v6[4];
    And int32_t is typedef int int32_t; mentioning that for clarity.

    Now when I attempt the following:
    printf("%u.%u.%u.%u\n", ut_addr_v6[0],
    ut_addr_v6[1], ut_addr_v6[2],
    ut_addr_v6[3]);

    I get: 2912244697.0.0.0

    Why is it that I get this result from the above?

    However, if I use the following:

    void *ptr;
    unsigned char *ptr1;

    ptr = ut_addr_info;
    ptr1 = ptr;

    printf("%u.%u.%u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);

    It displays how I wish it to display. So why is it that it works for a
    pointer to unsigned char but doesn't work when I attempt to display
    each element of the array of ints? And why is it that the array of its
    is 4 ints and not just one, when all it seems to occupy is one int? Is
    this for compensation of support for ipv6? If that is the case, then I
    must be accessing the individual bytes of the first element of the
    array with unsigned char *.

    Also, what is a good method/algorithm for converting network byte
    order to host byte order where host byte order is little endian? I can
    extract the individual bytes in reverse order and stoe them in an
    array but I was aiming more for storing them in an object of the same
    type I am manipulating.

    And lastly, when I was using the "ptr" of type void * above, I did the
    following:

    printf("%u.%u.%u.%u\n", (unsigned char)ptr[0],
    (unsigned char)ptr[1], (unsigned char)ptr[2],
    (unsigned char)ptr[3]);

    dereferencing of void *.

    Is this because the cast has no affect when ptr[0] is firstly
    converted to *(ptr+0) ? What is a way around this while still using a
    pointer to void?


    - nethlek
    Mantorok Redgormor, Oct 13, 2003
    #1
    1. Advertising

  2. On 12 Oct 2003 17:04:51 -0700, (Mantorok Redgormor)
    wrote:

    >I have a member of a struct which is:
    >int32_t ut_addr_v6[4];
    >And int32_t is typedef int int32_t; mentioning that for clarity.
    >
    >Now when I attempt the following:
    >printf("%u.%u.%u.%u\n", ut_addr_v6[0],
    > ut_addr_v6[1], ut_addr_v6[2],
    > ut_addr_v6[3]);


    Is it really part of a struct? You have no struct name in the code.

    Based on what you provided it seems as if an int is 32 bits. Is this
    correct?

    %u is the format for an unsigned int. You are passing a signed int.
    Normally a mismatch between format and argument causes undefined
    behavior.

    >
    >I get: 2912244697.0.0.0
    >
    >Why is it that I get this result from the above?
    >
    >However, if I use the following:
    >
    >void *ptr;
    >unsigned char *ptr1;
    >
    >ptr = ut_addr_info;
    >ptr1 = ptr;


    You could have simply said
    ptr1 = (void*)ut_addr_info;

    Which is it, ut_addr_info or ut_addr_v6?

    >
    >printf("%u.%u.%u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);


    Here the %u is correct because an unsigned char will be automatically
    promoted to unsigned int when used as an argument in a variadic
    function.

    But each ptr is only one char. (Pls confirm that on your system a
    char is 8 bits.) That means that ptr1[0] through ptr1[3] are the four
    bytes of ut_addr_v6[0].

    >
    >It displays how I wish it to display. So why is it that it works for a
    >pointer to unsigned char but doesn't work when I attempt to display
    >each element of the array of ints? And why is it that the array of its


    You are not printing the same data. In one case, you are printing the
    4 bytes of one int, each as a separate value. In the other, you are
    printing this one int in its entirety and three other int as well.

    >is 4 ints and not just one, when all it seems to occupy is one int? Is
    >this for compensation of support for ipv6? If that is the case, then I


    It is your code; how would we know.

    >must be accessing the individual bytes of the first element of the
    >array with unsigned char *.


    That is common for dealing with ip addresses.
    >
    >Also, what is a good method/algorithm for converting network byte
    >order to host byte order where host byte order is little endian? I can
    >extract the individual bytes in reverse order and stoe them in an
    >array but I was aiming more for storing them in an object of the same
    >type I am manipulating.


    If you are talking about unsigned int, you could use
    unsigned_int = ((unsigned)first_byte << 24) |
    ((unsigned)second_byte << 16) |
    ((unsigned)third_byte << 8) |
    fourth_byte;

    >
    >And lastly, when I was using the "ptr" of type void * above, I did the
    >following:
    >
    >printf("%u.%u.%u.%u\n", (unsigned char)ptr[0],
    > (unsigned char)ptr[1], (unsigned char)ptr[2],
    > (unsigned char)ptr[3]);
    >
    >dereferencing of void *.
    >
    >Is this because the cast has no affect when ptr[0] is firstly
    >converted to *(ptr+0) ? What is a way around this while still using a
    >pointer to void?


    Cast and [] have the same precedence and associate left to right so it
    should have been processed as ((unsigned char)ptr)[0] which never
    dereferences a void *. Post some compilable code so we can see for
    ourselves.


    <<Remove the del for email>>
    Barry Schwarz, Oct 13, 2003
    #2
    1. Advertising

  3. In article <>,
    (Mantorok Redgormor) wrote:

    > <snipped>


    I think you should take a course about programming in C.
    Christian Bau, Oct 13, 2003
    #3
  4. Mantorok Redgormor

    pete Guest

    Barry Schwarz wrote:
    >
    > On 12 Oct 2003 17:04:51 -0700, (Mantorok Redgormor)
    > wrote:


    > >printf("%u.%u.%u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);

    >
    > Here the %u is correct because an unsigned char will be automatically
    > promoted to unsigned int when used as an argument in a variadic
    > function.


    unsigned char will be converted to int,
    if int can represent all values of the original type.

    --
    pete
    pete, Oct 13, 2003
    #4
  5. On 13 Oct 2003 05:36:46 GMT, Barry Schwarz <> wrote:

    >On 12 Oct 2003 17:04:51 -0700, (Mantorok Redgormor)
    >wrote:
    >
    >>I have a member of a struct which is:
    >>int32_t ut_addr_v6[4];
    >>And int32_t is typedef int int32_t; mentioning that for clarity.
    >>
    >>Now when I attempt the following:
    >>printf("%u.%u.%u.%u\n", ut_addr_v6[0],
    >> ut_addr_v6[1], ut_addr_v6[2],
    >> ut_addr_v6[3]);

    >
    >Is it really part of a struct? You have no struct name in the code.
    >
    >Based on what you provided it seems as if an int is 32 bits. Is this
    >correct?
    >
    >%u is the format for an unsigned int. You are passing a signed int.
    >Normally a mismatch between format and argument causes undefined
    >behavior.
    >
    >>
    >>I get: 2912244697.0.0.0
    >>
    >>Why is it that I get this result from the above?
    >>
    >>However, if I use the following:
    >>
    >>void *ptr;
    >>unsigned char *ptr1;
    >>
    >>ptr = ut_addr_info;
    >>ptr1 = ptr;

    >
    >You could have simply said
    > ptr1 = (void*)ut_addr_info;
    >
    >Which is it, ut_addr_info or ut_addr_v6?
    >
    >>
    >>printf("%u.%u.%u.%u\n", ptr1[0], ptr1[1], ptr1[2], ptr1[3]);

    >
    >Here the %u is correct because an unsigned char will be automatically
    >promoted to unsigned int when used as an argument in a variadic
    >function.
    >
    >But each ptr is only one char. (Pls confirm that on your system a
    >char is 8 bits.) That means that ptr1[0] through ptr1[3] are the four
    >bytes of ut_addr_v6[0].
    >
    >>
    >>It displays how I wish it to display. So why is it that it works for a
    >>pointer to unsigned char but doesn't work when I attempt to display
    >>each element of the array of ints? And why is it that the array of its

    >
    >You are not printing the same data. In one case, you are printing the
    >4 bytes of one int, each as a separate value. In the other, you are
    >printing this one int in its entirety and three other int as well.
    >
    >>is 4 ints and not just one, when all it seems to occupy is one int? Is
    >>this for compensation of support for ipv6? If that is the case, then I

    >
    >It is your code; how would we know.
    >
    >>must be accessing the individual bytes of the first element of the
    >>array with unsigned char *.

    >
    >That is common for dealing with ip addresses.
    >>
    >>Also, what is a good method/algorithm for converting network byte
    >>order to host byte order where host byte order is little endian? I can
    >>extract the individual bytes in reverse order and stoe them in an
    >>array but I was aiming more for storing them in an object of the same
    >>type I am manipulating.

    >
    >If you are talking about unsigned int, you could use
    > unsigned_int = ((unsigned)first_byte << 24) |
    > ((unsigned)second_byte << 16) |
    > ((unsigned)third_byte << 8) |
    > fourth_byte;
    >
    >>
    >>And lastly, when I was using the "ptr" of type void * above, I did the
    >>following:
    >>
    >>printf("%u.%u.%u.%u\n", (unsigned char)ptr[0],
    >> (unsigned char)ptr[1], (unsigned char)ptr[2],
    >> (unsigned char)ptr[3]);
    >>
    >>dereferencing of void *.
    >>
    >>Is this because the cast has no affect when ptr[0] is firstly
    >>converted to *(ptr+0) ? What is a way around this while still using a
    >>pointer to void?

    >
    >Cast and [] have the same precedence and associate left to right so it
    >should have been processed as ((unsigned char)ptr)[0] which never
    >dereferences a void *. Post some compilable code so we can see for
    >ourselves.


    Disregard the last paragraph. Reading too quickly. It is a matter of
    precedence. [] has higher precedence than cast so this is interpreted
    as (unsigned char)(ptr[0]) which does in fact dereference the void
    pointer. You could have used ((unsigned char*)ptr)[0].


    <<Remove the del for email>>
    Barry Schwarz, Oct 14, 2003
    #5
    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. Replies:
    14
    Views:
    660
    Jonathan N. Little
    Dec 23, 2005
  2. Replies:
    1
    Views:
    366
  3. Mantorok Redgormor

    Representation of integers

    Mantorok Redgormor, Sep 13, 2003, in forum: C Programming
    Replies:
    10
    Views:
    623
    Kevin D. Quitt
    Sep 17, 2003
  4. Mantorok Redgormor

    displaying underlying representation of integers

    Mantorok Redgormor, Sep 26, 2003, in forum: C Programming
    Replies:
    5
    Views:
    348
  5. =?iso-8859-9?Q?Tongu=E7?= Yumruk

    Bitmask representation of integers

    =?iso-8859-9?Q?Tongu=E7?= Yumruk, Oct 8, 2003, in forum: Python
    Replies:
    3
    Views:
    861
    Scott David Daniels
    Oct 8, 2003
Loading...

Share This Page