When is an integer<->pointer cast actually correct?

后端 未结 15 2649
北荒
北荒 2020-12-13 07:48

The common folklore says that:

  • The type system exists for a reason. Integers and pointers are distinct types, casting between them is a malpractice in the m

15条回答
  •  眼角桃花
    2020-12-13 08:31

    You may need to access memory at a fixed known address, then your address is an integer and you need to assign it to a pointer. This is somewhat common in embedded systems. Conversely, you may need to print a memory address and thus need to cast it to integer.

    Oh, and don't forget you need to assign and compare pointers to NULL, which is usually a pointer cast of 0L

提交回复
热议问题