Assignment of address to integer variable

前端 未结 4 1033
萌比男神i
萌比男神i 2021-01-19 00:43

How come you can assign an address to an integer variable like this,the complier will not give an error. i always though you can only assign integer values to a integer vari

4条回答
  •  自闭症患者
    2021-01-19 01:17

    0x28ff1c is not an address itself - it's just a hexadecimal number.

    The following are equivalent:

    int a =   2686748;  //decimal number
    int a =  0x28ff1c;  //hexadecimal number
    int a = 012177434;  //octal number
    

    An address is represented by a pointer - if it's just that, an address, you can use a void*:

    void* p = (void*)0x28ff1c;
    

    In which case

    int a = p;
    

    wouldn't compile. p is an address, the number itself isn't.

提交回复
热议问题