What's the difference between void* and void**?

前端 未结 6 1544
别跟我提以往
别跟我提以往 2020-12-13 07:31

It\'s the special property that void* can also be assigned a pointer to a pointer and cast back and the original value is received.

I read this line

6条回答
  •  情话喂你
    2020-12-13 08:30

    void* is a pointer (or a pointer to the beginning of a unknown type array). void* is a pointer to the address of a pointer (or a pointer to the beginning of a 2D array).

    Also, if you are writing in C (and not in C++), then there is no by-reference parameter, only by value or by pointer.

    E.g.

    // By value C and C++
    void MyFunction(int a)
    {
    }
    
    // By pointer C and C++
    void MyFunction(int* a)
    {
    }
    
    // By reference C++ only
    void MyFunction(int& a)
    {
    }
    

    If you want a function that modifies the address of a pointer (e.g. void* ptr;)
    and enables the calling code to by affected by the change,
    then you need to pass a pointer by reference pass ptr to (void*&) and use ptr inside the function
    or pass a pointer to a pointer (void**) and pass &ptr and use *ptr inside the function.

提交回复
热议问题