Casting pointer to unsigned short

This is a discussion on Casting pointer to unsigned short within the C Programming forums, part of the General Programming Boards category; I have as input a char pointer: char* source. Assume that source contains the string "helloworld". Then we do this: ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Casting pointer to unsigned short

    I have as input a char pointer: char* source.
    Assume that source contains the string "helloworld".
    Then we do this:
    Code:
    unsigned short a = *((unsigned short*)source);
    I understand that source is a pointer to a character, so source is essentially just a 32-bit integer of some memory address in the system i.e. 0x001b5b15, and de-referencing it will give me "helloworld". But I'm not sure what happens when I cast it to unsigned short. I believe that it will truncate the first 2 bytes and return only the last 2 bytes: 0x00005b15 and de-referencing this memory address will return some unknown value. Is my understanding correct?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by cks2k2 View Post
    I have as input a char pointer: char* source.
    Assume that source contains the string "helloworld".
    Then we do this:
    Code:
    unsigned short a = *((unsigned short*)source);
    I understand that source is a pointer to a character, so source is essentially just a 32-bit integer of some memory address in the system i.e. 0x001b5b15, and de-referencing it will give me "helloworld". But I'm not sure what happens when I cast it to unsigned short. I believe that it will truncate the first 2 bytes and return only the last 2 bytes: 0x00005b15 and de-referencing this memory address will return some unknown value. Is my understanding correct?
    No.

    Address is not changed.
    Dereferencing it as char will not give you "helloworld".
    It will give you 'h'
    When you cast it to unsigned short* - it will be same address - but the destination will be split in two-chars patterns istead of 1
    so dereferencing it will bring
    ('h' << 8) | 'e'

    or

    ('e' << 8) | 'h'

    depending on the endianess
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    I just wanted to try to make vart's explanation a bit closer to spoken language

    Eg, the pointer type tells how many bits will be read from the address it points to.

    With char pointer, we will read 8 bits (one byte).
    When we cast the char pointer to short int pointer, tha data pointer points to is assumed to be short integer.
    Now dereferencing short int pointer will give you 16 bits (2 bytes) which is the size of a short.

    The role endianess plays is in how processor handles the data. There is two types of systems, little and big endianess systems. In little endianess system, the least significant bytes (note not bits, bytes) will be first. In big endianess machines the most significant bytes are first.

    so, number short integer 1, divided into bytes will be like 0x00 0x01 in big endianess machines memory.
    little endianess machine has it as 0x01 0x00

    Because chars are just one byte wide, they will be stored similarly in both big and little endianess machines. But when we store 2 chars, lets say 0x01 and 0x00 in continuous memory block, and then read them back as short integer, we'll get different value depending on architecture. This can be done by casting pointer to char array to pointer to short.

    Eg, we place data as 0x0100 in array, now when big endianess machine reads this as short, it interpretes value to be 0x100 (256 in decimal notation)

    When little endianess machine reads the data, it is interpreted to mean value 0x1 (1 in decimal notation)

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Hmmm... so by casting to unsigned short* I will be reading 2 chars (2 bytes) at a time instead of 1 (size of char)?

    The problem I'm trying to solve is that the char* source specifies a string length in the first 2 bytes followed by the string itself, and I need to get that length first. I tried experimenting with source = "06cks2k2" with 06 being the first 2 bytes and "cks2k2" being the string.
    But I am not getting 6 as the string length but 13872. Why?

    Code:
    char* source = "06cks2k2";
    unsigned int len = *((unsigned short*) source);  // get 13872, not 06?

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    '0' character is not the same as 0 integer

    int length = (source[0] - '0') * 10 + (source[1] - '0')
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Again as Vart explained, characteric "06" is not same as 0x00 0x06. You can google for ASCII table, to see what are the actual values in memory, representing characters.

    Besides, assuming you use PC, you will propably be using little endian system. So if you have chars 0x00 0x06 in memory, it will be interpreted as short 0x0600. You should have 0x06 0x00 as the lenght (which would be interpreted as short 0x0006).

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Doing a decimal to hex translation of 13872 proves Maz right: The value is 0x3630, which is '60'.

    You probably can't use this little trick in a way that is meaningful, you simply have to read each digit and translate to an integer (subtract '0' and multiply by 10 then add second digit).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. A Question About Unit Testing
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 08:22 PM
  3. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM
  4. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 02:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21