c - 在 C 中将双指针作为参数传递

标签 c struct

假设有一个 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_infomyStruct_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/

相关文章:

c - 如何使用 CDT 在 eclipse 中进行运行配置

swift - Swift 3 中相互引用的结构

c++ - 深度复制结构

c - 了解作用域后堆栈使用错误

c - 创建了多少进程

c++ - 固定大小结构

c - 结构初始化失败

go - 如何在 golang 中使用动态/任意字段创建结构/模型

c - 是否有 GNU getline 接口(interface)的替代实现?

c - malloc 之后出现段错误 char**