如何将值传递给指针参数C ++

阿德里安娜(Adriansyah)

这是示例代码

void test(void *outputData)
{
   u8 *changeData;
   changeData[1] = 'T';
   changeData[2] = 'M';
}

void main()
{
   u8* const buf = (u8*) malloc(36654);
   test(buf);
}

所以我想做的就是将changedata返回到buf

我在测试功能中尝试过此方法,但似乎不起作用

*outputData = *changeData

编辑:

我正在尝试访问我在测试功能中修改的主要功能上的buf

提前致谢

WhozCraig

以下代码中的注释。错误或不明智的代码清单很多。诚然,由于您的问题帖子尚不完全清楚,所以这是一个最好的建议,但可能与您的要求很接近。如果不....

#include <iostream>

// not specified in your question code. assumed to come from somewhere
typedef unsigned char u8;

void test(void *outputData)
{
    // C allows implicit casting from void*; C++ does not.
    u8 *changeData = reinterpret_cast<u8*>(outputData);

    // C and C++ both use zero-based indexing for arrays of data
    changeData[0] = 'T';
    changeData[1] = 'M';
    changeData[2] = 0;
}

// void is not a standard supported return type from main()
int main()
{
    // in C++, use operator new, not malloc, unless you have
    //  a solid reason to do otherwise (and you don't)
    u8* const buf = new u8[3];
    test(buf);

    // display output
    std::cout << buf << '\n';

    // delete[] what you new[], delete what you new.
    delete[] buf;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章