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

前端 未结 6 1547
别跟我提以往
别跟我提以往 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:18

    if you want to store some pointer or anything you will probably use void*.

    However if you want to write a function which can be able to initialize this magic pointer, then you need pass this argument to this function as void**

    void   fun1();
    int    fun2(int);
    double fun3(long);
    bool   fun4(int, long, double);
    
    
    int rand(void** pp)
    {
      switch(time()%4)
      {
        case 0: *pp = fun1; return 0;
        case 1: *pp = fun2; return 1;
        case 2: *pp = fun3; return 2;
        case 3: *pp = fun4; return 3;
      }
    }
    
    int main()
    {
        void* pointer;
        int funId;
    
        funId = rand(&pointer);
    
        setCallback(pointer, funId);
    }
    

提交回复
热议问题