Why does the void pointer point to anything?

When something is cast for void, the value becomes NULL. However, why does void * point to any data type? Should a pointer to emptiness be useless?

+4
source share
3 answers

The void pointer is a pointer to anything. This is a generic pointer that does not have a specific type. It may also matter NULL, in which case it does not indicate anything. To use the void pointer, you must keep track of what it actually points to, and when you intend to use it, you must apply it to the appropriate type.

They can be dangerous, because if you apply it to the wrong type, it will lead to undefined behavior at runtime.

+6
source

When something is cast for void, the value becomes NULL.

Not true. Nothing happens with the specified data.

Should a pointer point to emptiness?

The void pointer is the closest to the fact that C has a "common type" and is very useful in that it allows things like several common functions, several common containers, etc.

+3
source

void * - . , .

void * .

+2

Source: https://habr.com/ru/post/1532381/


All Articles