假设有一个如下的API:

void myAPI(int8** ptr)


如果我要将结构的指针传递给此函数,则这是我的代码:

typedef myStruct {
    int a;
};

myStruct *ptr = NULL;
memset(ptr, 0, sizeof(myStruct));

myAPI((int8**)&ptr);


我的问题是,如果我现在不使用ptr,则该怎么办?

myStruct myStruct_info;
memset(&myStruct_info, 0, sizeof(myStruct));


我还会做myAPI((int8**)&myStruct_info)吗?

最佳答案

Would I also be doing myAPI((int8**)&myStruct_info)?


简短的回答是没有你不能。

&myStruct_info是您的myStruct_info变量的地址。
myAPI函数的参数需要指向变量的指针的地址。

如果您说int8 ** a = (int8**)&myStruct_info;,则:

a - holds the address of myStruct_info
*a - the value of myStruct_info




**a - means that you take the value stored in myStruct_info and use it as a pointer - REALLY BAD -

关于c - 在C中将双指针作为参数传递,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50339581/

10-11 22:49