When to use a void pointer?

后端 未结 13 2008
北荒
北荒 2020-12-07 17:33

I understand the use of void pointer for malloc implementation.

void* malloc  ( size_t size );

Can anyone suggest other reasons or provide

13条回答
  •  离开以前
    2020-12-07 18:04

    void pointers should be used any time the contents of a block of data is not important. For example when copying data the contents of a memory area is copied but the format of the data is not important.

    For functions that operate on blocks of memory without needing to understand the contents using void pointers clarifies the design to users so that they know the function does not care for any data format. Often functions a coded to take a char * to handle blocks of memory when the function is actually content agnostic.

提交回复
热议问题