Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a void pointer point to anything?

Tags:

c

When something is cast to void, then the value becomes NULL. However, why does a void * point to any data type? Shouldn't a void pointer just be useless?

like image 260
Edward Karak Avatar asked Oct 12 '25 07:10

Edward Karak


1 Answers

A void pointer is a pointer to anything. It is a generic pointer that doesn't have a particular type. It can also have the value NULL in which case it doesn't point to anything. To use a void pointer, you have to keep track of what it actually points to and when you are going to use it, you must cast it to the appropriate type.

They can be dangerous, because if you cast it to the wrong type, it will result in undefined behavior at runtime.

like image 198
edtheprogrammerguy Avatar answered Oct 13 '25 22:10

edtheprogrammerguy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!