Thread: int split...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    int split...

    i have the next code:

    Code:
    unsigned short int port_n;
    
    port_n = htons(6667);
    
    printf("%x\n%x", (char) port_n,  (char) *(&port_n+1));
    what i'm trying to do is to split the 2 bytes of unsigned short int into two single chars. the thing is, *(&port_n+1) does not give me the right value.
    int is 2 bytes long, what am i doing wrong?




    thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       unsigned short int port_n = 0x1234;
       printf("%x\n%x\n", *(char*)&port_n,  *((char*)&port_n + 1));
       return 0;
    }
    
    /* my output
    34
    12
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    can you please explain why: *((char*)&port_n + 1)
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    casting the address of port_n to a character pointer allows you to move to an individual byte since sizeof(char) == 1 always.

    The reason yours didn't work is because whenever you do pointer arthimatic you are not just adding a stright value but that value times the size of the object. So in your case its really (address of port_n + 2)

  5. #5
    Quote Originally Posted by Devil Panther
    can you please explain why: *((char*)&port_n + 1)
    '&port_n' is the address of port_n. Its type is 'short*'.

    '(char*)' is a typecast that changes the type of the address to 'char *'.

    '+1' adds one the this address (it's now a char*)

    '*(<expression>)' is the dereferencement operator. It allow to reach the pointed value.
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    27

    union

    Why not ??

    union intchar
    {
    short int i;
    char cs[2];
    };
    Pappy
    You learn something new everyday.

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    So in your case its really (address of port_n + 2)
    i'm sorry but it's wrong.


    if port_n was a pointer, i would have understood it as:

    &port_n - the address of the pointer port_n.

    by doing (char *) &port_n i get the addess of the value.

    so by increasing the (char *) &port_n by one byte i get the address of next cell from *port_n... and then i get it's value:

    * ( (char *)&port_n + 1 ) = 0x12



    but since port_n is not a pointer, i don't understand it
    Last edited by Devil Panther; 07-23-2004 at 10:48 AM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    but
    Code:
    &port_n
    is a pointer

  9. #9
    Quote Originally Posted by Devil Panther
    i'm sorry but it's wrong.
    I don't think so.
    if port_n was a pointer, i would have understood it as:

    &port_n - the address of the pointer port_n.
    You think wrong. 'port_n' is an object. Like any object, it has an address &<object>. A pointer is just another object, but it's not necessary to have one there. Actually, according to your original post, this object is a short.
    by doing (char *) &port_n i get the addess of the value.
    Bad wording. A value is not an object. It has no address at all.
    Read my post again, and don't change the words. The semantics is not an option. Every word is important. If there is one you don't understand, please ask for details. Don't assume. Be sure.
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #10
    Quote Originally Posted by Thantos
    but
    Code:
    &port_n
    is a pointer
    According to the defintion given by the original poster, port_n is a short, hence &port_n is a value of type short*. Eventually it is a pointer constant of type short*.

    A pointer is just another object. It has an address. It happens that the value of a pointer is also an address.

    &object has no address (&&object is plain wrong like are &NULL or &0 or &((char*)123)).
    Emmanuel Delahaye

    "C is a sharp tool"

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I try to keep things simple in my explantions which is why I said what I said

  12. #12
    Quote Originally Posted by Thantos
    I try to keep things simple in my explantions which is why I said what I said
    I understand your concern, but the problem is that willing to be too simple leads to misunderstanding. Pointers are a difficult matter where the meaning of the words is essential to be consistent.

    There is a big difference between a pointer (object) and a pointer constant (constant). I think important to have it clear in mind
    Emmanuel Delahaye

    "C is a sharp tool"

  13. #13
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    According to the defintion given by the original poster, port_n is a short, hence &port_n is a value of type short*. Eventually it is a pointer constant of type short*.
    but &port_n is the address of the value...


    There is a big difference between a pointer (object) and a pointer constant (constant). I think important to have it clear in mind
    can you please explain me the difference?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    but &port_n is the address of the value...
    As said &port_n is the address of the object which in turns holds the value.

    One difference between &<object> and type * is that in &<object> you can't change the value of the pointer itself (ie: (&<object>)++) while in a pointer you can.

    I see your point Emmanuel.
    Last edited by Thantos; 07-23-2004 at 11:30 AM.

  15. #15
    Quote Originally Posted by Devil Panther
    but &port_n is the address of the value...
    I told you already that this is plain wrong. Don't you read the responses you received? A value has no address.

    What is the address of 'a' or 123 ?

    Quote Originally Posted by -ed-
    difference between a pointer (object) and a pointer constant (constant)
    can you please explain me the difference?
    A pointer is an object (an other name for variable). It has a memory representation, it occupies a certain number of consecutive bytes. It has a memory location know as 'address'.

    The value is the contents of the object (the status of the bits in the bytes of the object). It happens that the kind of value that a pointer holds is an address (the memory location of some other object). Some other objects hold integers, floating points, string of characters... Whatever.

    A constant is a literal value, also know as immediate value. It also can be the result of a constant expression.

    123 is an integer constant.
    'H' is a character constant
    (void*)0 is a pointer constant (aka the null pointer)
    123.456 is a floating point constant
    sizeof (double) is a constant expression
    int x; is an object, but
    &x is a pointer constant .
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM