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

Casting int to void * is rather meaningless and should not be done as you would be attempting to cast a non-pointer to a pointer. Quoting the C99 standard, section 6.3.2.3 item 5:

An integer may be converted to any pointer type. Except as previously
specified, the result is implementation-defined, might not be
correctly aligned, might not point to an entity of the referenced
type, and might be a trap representation.

You could cast int * to void * (any pointer is convertible to void * which you can think of like the “base type” of all pointers).

Casting void * to int is not portable and may be completely wrong depending on the platform you use (e.g. void * maybe 64 bits wide and int may only be 32 bits). Quoting the C99 standard again, section 6.3.2.3 item 6:

Any pointer type may be converted to an integer type. Except as
previously specified, the result is implementation-defined. If the
result cannot be represented in the integer type, the behavior is
undefined. The result need not be in the range of values of any
integer type.

To solve this, some platforms provide uintptr_t which allows you to treat a pointer as a numeric value of the proper width.

Leave a Comment