When to use a void pointer?

后端 未结 13 2004
北荒
北荒 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:26

    It is commonly used in numerical code, for example a C root solver function might look like that:

    double find_root(double x0, double (*f)(double, void*), void* params)
    {
    /* stuff */
    y = f(x, params);
    /* other stuff */
    }
    

    params is cast by f to some structure it knows about, but find_root doesn't.

    0 讨论(0)
提交回复
热议问题