What is (void*) casting used for?

前端 未结 4 1871
有刺的猬
有刺的猬 2021-02-09 12:11

I did try searching for this on Stack Overflow, but I think due to the syntax and not knowing exactly what to search I became a little unstuck.

I have seen (v

4条回答
  •  广开言路
    2021-02-09 12:43

    void*, usually referred to as a void pointer, is a generic pointer type that can point to an object of any type. Pointers to different types of objects are pretty much the same in memory and so you can use void pointers to avoid type checking, which would be useful when writing functions that handle multiple data types.

    Void pointers are more useful with C than C++. You should normally avoid using void pointers and use function overloading or templates instead. Type checking is a good thing!

提交回复
热议问题