what is meant by (void *) variable and (void *) & variable in C [closed]

That is to consider an integer value as a pointer (expecting to access the memory pointed out by that integer value), nothing else.

However, this is not a very good practice. Quoting the C11, chapter §

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.

For a safer integer to pointer (and vice-versa) conversion, there are two types defined explicitly, intptr_t and uintptr_t. From chapter §

The following type designates a signed integer type with the property that any valid
pointer to void can be converted to this type, then converted back to pointer to void,
and the result will compare equal to the original pointer:


and it’s unsigned counterpart, uintptr_t.

The standard makes these types as optional, so, again, no guarantee of availability across all implementations.

However, regarding the (void *) &variable is to consider a pointer to a certain type as a pointer to void. This sometimes can be useful, if the pointer is later converted to the original type before dereferencing. That is perfectly defined behavior. Quoting the standard again (emphasis mine)

A pointer to an object type may be converted to a pointer to a different object type. If the
resulting pointer is not correctly aligned for the referenced type, the behavior is
undefined. Otherwise, when converted back again, the result shall compare equal to the
original pointer.

Browse More Popular Posts

Leave a Comment