如何使 void* 参数将其本地结果保存在使用 struct * 调用的函数中?

发布于 2024-12-13 00:16:47 字数 469 浏览 0 评论 0原文

如何使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-12-20 00:16:47

函数参数必须是 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 use f ( &callingPtr ); .

hth

Mario

戈亓 2024-12-20 00:16:47

您的代码不是正确的 C++,无法编译,但我已尽力翻译。您正在尝试为结构分配空间并将其存储在 ptr 中,以便在调用函数中使用,对吗?

如果是这样,我相信这段代码可以满足您的需求。

typedef struct _tag_myStruct
{
    std::string myStr;
} myStruct;

void f(void **ptr, const char* myChar)
{
    myStruct *myStruct1 = new (myStruct);

    myStruct1->myStr = myChar;

    // write results in ptr 
    // Note we dereference **ptr here to store the memory location
    //   in the pointer variable in the calling function (main() in this case)
    *ptr = (void *)myStruct1;
}

int main()
{
    myStruct *struct1 = NULL;
    myStruct *struct2 = NULL;

    f((void **)&struct1, "test1");
    f((void **)&struct2, "test2");

    std::cout << struct1->myStr.c_str() << std::endl;
    std::cout << struct2->myStr.c_str() << std::endl;
} 
test1
test2
Press any key to continue . . .

编辑以获得更多说明

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.

typedef struct _tag_myStruct
{
    std::string myStr;
} myStruct;

void f(void **ptr, const char* myChar)
{
    myStruct *myStruct1 = new (myStruct);

    myStruct1->myStr = myChar;

    // write results in ptr 
    // Note we dereference **ptr here to store the memory location
    //   in the pointer variable in the calling function (main() in this case)
    *ptr = (void *)myStruct1;
}

int main()
{
    myStruct *struct1 = NULL;
    myStruct *struct2 = NULL;

    f((void **)&struct1, "test1");
    f((void **)&struct2, "test2");

    std::cout << struct1->myStr.c_str() << std::endl;
    std::cout << struct2->myStr.c_str() << std::endl;
} 
test1
test2
Press any key to continue . . .

edited for a bit more clarification

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文