Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

While it has been common practice to use integers and pointers interchangeably in C, the C99 standard states that pointer to integer and integer to pointer conversions are implementation - defined.

Wiki Markup
According to C99 \[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\], the only value whichthat can be considered interchangeable between pointers and integers is the constant 0. Except in this case, conversions between integers and pointers may have undesired consequences depending on the implementation:

...

A union can be used to give raw memory access to both an integer and a pointer. This is an efficient approach, as the structure only requires as much storage as the larger of the two fields.

...

It is sometimes necessary in low level kernel or graphics code to access memory at a specific location, requiring a literal integer to pointer to conversion. In this non-compliant code, a pointer is set directly to an integer constant, where it is unknown whether the result will be as intended.

...

The result of this assignment is implementation - defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

...

Converting from pointer to integer or vice - versa results in unportable code and may create unexpected pointers to invalid memory locations.

...