what does it mean to convert int to void* or vice versa?

前端 未结 6 1088
广开言路
广开言路 2020-11-27 07:13

What does it mean to convert an integer value to a void* or viceversa from a memory point of view? My understanding is void* is an address to a blo

6条回答
  •  一生所求
    2020-11-27 07:21

    This is quite like comparing apples and oranges. The only way that this code works is because you are explicitly casting it back and forth.

    The C compiler is just copying the bytes from the int into the space for the pointer and back, just like how you can hold a char in an int.

    This could even cause your number to be messed up if for some reason an 'int' is longer than a 'void *' on your platform.

    If you actually do want have a number to convert from integers into pointers and back, look into intptr_t. That type is actually designed to store both integers and pointers.

提交回复
热议问题