简体   繁体   English

(void **)在C中的含义是什么?

[英]What does (void**) mean in C?

I would look this up, but honestly I wouldn't know where to start because I don't know what it is called. 我会仔细研究,但说实话,我不知道从哪里开始,因为我不知道它叫什么。 I've seen variables passed to functions like this: 我已经看到传递给这样的函数的变量:

myFunction((void**)&variable);

Which confuses the heck out of me cause all of those look familiar to me; 这让我感到困惑,因为所有这些对我来说都很熟悉; I've just never seen them put together like that before. 我以前从未见过他们像以前那样放在一起。

What does it mean? 这是什么意思? I am a newb so the less jargon, the better, thanks! 我是一个新手,所以行话越少越好,谢谢!

void* is a "pointer to anything". void*是“指向任何东西的指针”。 void ** is another level of indirection - "pointer to pointer to anything". void **是另一个间接层 - “指向任何东西的指针”。 Basically, you pass that in when you want to allow the function to return a pointer of any type. 基本上,当你想让函数返回任何类型的指针时,你会传入它。

&variable takes the address of variable. &variable&variable的地址。 variable should already be some kind of a pointer for that to work, but it's probably not void * - it might be, say int * , so taking its address would result in a int ** . variable应该已经是某种指针才能工作,但它可能不是void * - 它可能是,比如int * ,所以取其地址会产生一个int ** If the function takes void ** then you need to cast to that type. 如果函数采用void **则需要转换为该类型。

(Of course, it needs to actually return an object of the right type, otherwise calling code will fail down the track when it tries to use it the wrong way.) (当然,它需要实际返回一个正确类型的对象,否则调用代码会在尝试以错误的方式使用它时失败。)

It's a cast to a pointer to a void pointer. 它是指向void指针的指针。

You see this quite often with functions like CoCreateInstance() on Windows systems. 您经常使用Windows系统上的CoCreateInstance()等函数来查看。

ISomeInterface* ifaceptr = 0;
HRESULT hr = ::CoCreateInstance(CLSID_SomeImplementation, NULL, CLSCTX_ALL,
    IID_ISomeInterface, (void**)&ifaceptr);
if(SUCCEEDED(hr))
{
    ifaceptr->DoSomething();
}

The cast converts the pointer to an ISomeInterface pointer into a pointer to a void pointer so that CoCreateInstance() can set ifaceptr to a valid value. ISomeInterface转换将指向ISomeInterface指针的指针转换为指向void指针的指针,以便CoCreateInstance()可以将ifaceptr设置为有效值。

Since it is a pointer to a void pointer, the function can output pointers of any type, depending on the interface ID (such as IID_ISomeInterface). 由于它是指向void指针的指针,因此该函数可以输出任何类型的指针,具体取决于接口ID(例如IID_ISomeInterface)。

It's a pointer to a pointer to a variable with an unspecified type. 它是指向具有未指定类型的变量的指针。 All pointers are the same size, so void* just means "a pointer to something but I have no idea what it is". 所有指针都是相同的大小,所以void*只是意味着“指向某个东西的指针,但我不知道它是什么”。 A void** could also be a 2D array of unspecified type. void**也可以是未指定类型的2D数组。

Take it apart piece by piece... 把它分开一块一块......

myFunction takes a pointer to a pointer of type void (which pretty much means it could point to anything). myFunction获取一个指向void类型指针的指针(这几乎意味着它可以指向任何东西)。 It might be declared something like this: 可能会声明如下:

myFunction(void **something);

Anything you pass in has to have that type. 你传递的任何东西都必须有这种类型。 So you take the address of a pointer, and cast it with (void**) to make it be a void pointer. 因此,您获取指针的地址,并使用(void **)将其转换为使其成为void指针。 (Basically stripping it of any idea about what it points to - which the compiler might whine about otherwise.) (基本上剥夺了它所指出的任何想法 - 编译器可能会抱怨其他的。)

This means that &variable is the address (& does this) of a pointer - so variable is a pointer. 这意味着&variable是指针的地址(并执行此操作) - 因此变量是指针。 To what? 要什么? Who knows! 谁知道!

Here is a more complete snippet, to give an idea of how this fits together: 这是一个更完整的代码片段,让您了解它是如何组合在一起的:

#include <stdio.h>

int myInteger = 1;
int myOtherInt = 2;
int *myPointer = &myInteger;

myFunction(void **something){
    *something = &myOtherInt;
}

main(){
    printf("Address:%p Value:%d\n", myPointer, *myPointer);
    myFunction((void**)&myPointer);
    printf("Address:%p Value:%d\n", myPointer, *myPointer);
}

If you compile and run this, it should give this sort of output: 如果你编译并运行它,它应该给出这种输出:

Address:0x601020 Value:1
Address:0x601024 Value:2

You can see that myFunction changed the value of myPointer - which it could only do because it was passed the address of the pointer. 你可以看到myFunction改变了myPointer的值 - 它只能这样做,因为它传递了指针的地址。

That casts &variable to a void** (that is, a pointer to a pointer to void ). 这会将&variable转换为void** (即指向void的指针)。

For example, if you have something along the lines of 例如,如果您有某些内容

void myFunction(void** arg);

int* variable;

This passes the address of variable (that's what the unary- & does, it takes the address) to myFunction() . 这会将variable的地址(这是一元&并且执行,它需要地址)传递给myFunction()

The variable is a pointer to something of undefined (void) type. 变量是指向未定义(void)类型的指针。 The & operator returns the address of that variable, so you now have a pointer to a pointer of something. &运算符返回该变量的地址,因此您现在有一个指向某事物指针的指针。 The pointer is therefore passed into the function by reference. 因此,指针通过引用传递给函数。 The function might have a side effect which changes the memory referenced by that pointer. 该函数可能具有副作用,该副作用会更改该指针引用的内存。 In other words, calling this function might change the something that the original pointer is referencing. 换句话说,调用此函数可能会更改原始指针引用的内容。

#include <stdio.h>
#include <iostream>

int myInteger = 1;
std::string myOtherInt = "Test";
int *myPointer = &myInteger;

void myFunction(void **something)
    {
        *something = &myOtherInt;
    }

int main(){
    //printf("%d\n", *myPointer);
    printf("Address:%p Value:%d\n", myPointer, *myPointer);
    myFunction((void**)&myPointer);
    printf("Address:%p Value:%d\n", myPointer, *myPointer);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM