What is going on here?

This is a discussion on What is going on here? within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me what is going on in this code: Code: int main() { unsigned short shortint = 0x4041; ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    11

    Question What is going on here?

    Can anyone tell me what is going on in this code:

    Code:
    int main()
    {
        unsigned short shortint = 0x4041;
        unsigned char *output = (unsigned char *) shortint + NULL;
        for (int i=0; i<26; i++)
        {
            unsigned char chr = (unsigned char) &output[i];
            if (chr == NULL)
            {
                cout << "NULL" << endl;
                break;
            }
            else
                cout << i << " = " << chr << endl;
        }
    }
    It was supposed to split a short into bytes but instead output the letters A to Z.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    It was supposed to split a short into bytes but instead output the letters A to Z.
    What were you expecting the output to be?

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    I was expecting:

    0 = H
    1 = I
    NULL

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    I don't see what the loop is doing there.
    Code:
    int main ()
    {
    	unsigned short shortint = 0x4041;
    	unsigned char* output = (unsigned char*) &shortint;
    
    	cout << output[0] << " " << output[1] << endl;
    
    	return 0;
    }
    Also, '\x40' == @ and '\x41' == A so I don't see why you're expecting H and I.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Can you explain that? The hexadecimal ascii code for H is 48, and for I its 49. So how are you supposed to get that from 4041?

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    sorry, shoud be:

    0 = @
    1 = A
    NULL

    Doing what you suggest crashes my programming, not sure why. And I also can't work out why my program out puts:

    0 = A
    1 = B
    2 = C
    ...etc.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    My mistake again! I had just forgotten to put the '&' in.
    Still doesn't explain my strange results in my original program.
    Last edited by nectodn; 02-07-2006 at 10:06 AM.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Well, let's move on to this line:
    Code:
    unsigned char *output = (unsigned char *) shortint
    As far as I can tell, you are telling the compiler you want 0x4041 to be interpreted as an address in memory(where supposedly there will be a char array). Why do you expect that at address 0x4041 the compiler will find a char array? I imagine that 0x4041 is unlikely to be a legal address, and even if it was legal the chances of finding a char array at some random address are probably slim to none.
    Last edited by 7stud; 02-07-2006 at 10:13 AM.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    Ahh. That was my mistake, it wasn't supposed to be a memory address. Not very good with pointers.

    Though if it was always pointing at a memory address how come the program consistently gave me the same results and is it just a coincidence that the first char was always one more in value than the first char in shortint and that it always output, in order, the rest of the alphabet?

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    A pointer to a char array stores the address of the first character in the char array. The rest of the array is layed out in contiguous memory next to the address of the first char. Since &output = &output[0] = 0x4041, then output[1] is at address 0x4042, and output[2] is at address 0x4043.

    Then in this line:
    Code:
    unsigned char chr = (unsigned char) &output[i]
    you cast the address of output[i] to an unsigned char. An unsigned char is only 1 byte, or 8 bits, yet the address of output[i] is 2 bytes or 16 bits. So, when you cast from 16 bits to 8 bits, the first 8 bits are used, e.g.

    &output[0] = 0x4041 --> 0x41
    &output[1] = 0x4042 --> 0x42
    &output[2] = 0x4043 --> 0x43
    etc.

    And,

    0x41 = 'A'
    0x42 = 'B'
    0x43 = 'C'
    Last edited by 7stud; 02-07-2006 at 10:57 AM.

Popular pages Recent additions subscribe to a feed

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