如何使 void* 参数将其本地结果保存在使用 struct * 调用的函数中?
如何使 void*
参数将其本地结果保存在函数中,该函数使用 struct *
进行调用。
struct1{};
struct2{};
void f(void * ptr, const char* myChar)
{
struct struct1 *myStruct1 = new (struct struct1);
// write results in ptr
ptr = (void*)&myStruct1; // the result held by ptr is "" empty.
}
int main()
{
f(struct1*, char* char1);
f(struct2*, char* char1);
}
即使使用不同的参数调用 f
,如何使 ptr
保留结果?
How to make a void*
argument hold its local result in a function, which is called with a struct *
.
struct1{};
struct2{};
void f(void * ptr, const char* myChar)
{
struct struct1 *myStruct1 = new (struct struct1);
// write results in ptr
ptr = (void*)&myStruct1; // the result held by ptr is "" empty.
}
int main()
{
f(struct1*, char* char1);
f(struct2*, char* char1);
}
How can I make ptr
hold the result even though f
is called with a different argument?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
函数参数必须是
void** ptr
,以便您可以在函数中将*ptr
设置为该值。当您传递参数时,请使用f ( &callingPtr );
。马里奥
The function argument needs to be a
void** ptr
so you can set*ptr
in your function to the value. When you pass the argument usef ( &callingPtr );
.hth
Mario
您的代码不是正确的 C++,无法编译,但我已尽力翻译。您正在尝试为结构分配空间并将其存储在 ptr 中,以便在调用函数中使用,对吗?
如果是这样,我相信这段代码可以满足您的需求。
编辑以获得更多说明
Your code isn't proper C++ and doesn't compile, but I've done my best to translate. You're trying to allocate space for a structure and store it in ptr, to be used in the calling function, correct?
If so, this code does what I believe you're looking for.
edited for a bit more clarification