My char pointer points to invalid value after being cast from int*

前端 未结 7 672
余生分开走
余生分开走 2021-02-03 17:19

I am learning C programming language, I have just started learning arrays with pointers. I have problem in this question, I hope the that output must be

7条回答
  •  一生所求
    2021-02-03 17:56

    Assumed a little endian architecture where an int is 32 bits (4 bytes), the individual bytes of int arr[] look like this (least significant byte at the lower address. All values in hex):

    |01 00 00 00|02 00 00 00|03 00 00 00|04 00 00 00|05 00 00 00
    
    char *ptr = (char *) arr;
    

    Now, ptr points to the first byte - since you have casted to char*, it is treated as char array onwards:

    |1|0|0|0|2|0|0|0|3|0|0|0|4|0|0|0|5|0|0|0
     ^
     +-- ptr
    

    Then, *(ptr+4) accesses the fifth element of the char array and returns the corresponding char value:

    |1|0|0|0|2|0|0|0|3|0|0|0|4|0|0|0|5|0|0|0
             ^
             +-- *(ptr + 4) = 2
    

    Hence, printf() prints 2.

    On a Big Endian system, the order of the bytes within each int is reversed, resulting in

    |0|0|0|1|0|0|0|2|0|0|0|3|0|0|0|4|0|0|0|5
             ^
             +-- *(ptr + 4) = 0
    

提交回复
热议问题