how can I use void** function(void**)

void * can refer to an address of any type. You can read it as: 'a pointer to something in memory'. So this means, it can point to an integer, a string, an object, ... everything. Some examples:

void *anyObject;
int a = 1;
float b = 2;
char *c = "somestring";

function aFunction(int a)
{
   ...
}

anyObject = &a;
anyObject = &b;
anyObject = &c;
anyObject = &aFunction;

These are all valid assignments.

To come back to your question: a void ** is a pointer to a pointer to void; in proper English: it points to a location in memory where the value of void * is stored. Continuing from the above example:

void **p;
p = &anyObject;

In general, void * are dangerous, because they are not type safe. In order to use them, you MUST cast them to a known type. And the compiler will not complain if you cast it to something illegal.

Okay, how do you use it? Well, that is difficult to answer because we cannot derive from the declaration what types are expected. Also, it is possible that this functions allocates memory itself inside the function and returns that within the parameter. But, the general principle is: take the address (&) of 'a pointer to something', and pass that to the function.

int a = 0;
int *pa = &a;
functionName(&pa);

猜你喜欢

转载自blog.csdn.net/qq_29094161/article/details/76450718