【问题标题】:How to let a uint8_t const* point at the same address as a char*?如何让 uint8_t const* 指向与 char* 相同的地址?
【发布时间】:2012-04-10 19:53:37
【问题描述】:

在 C++ 程序中,我有一个 char* 指向包含 BUFFER_SIZE 个字符的数组的开头(每个字符的大小为一个字节)。 我现在想在 NS3 模拟中使用该代码,数据包将 uint8_t const* 作为输入,指向缓冲区。

我应该怎么做才能创建一个指向第一个提到的缓冲区的 'uint8_t const*'?

【问题讨论】:

    标签: c++ ns-3


    【解决方案1】:

    您必须使用reinterpret_cast:

    int main () {
     char buffer[10];
     reinterpret_cast<unsigned char const *>(buffer);
    }
    

    或使用 C 风格的演员表:

    int main () {
     char buffer[10];
     (unsigned char const *)buffer;
    }
    

    【讨论】:

    • const_cast 在这种情况下是必要的还是依赖于编译器? codepad.org/nDZCA2zX
    • 两者都不是。根本不需要。我一定是在测试时弄糊涂了。我已经更新了我的答案。谢谢!
    猜你喜欢
    • 2020-03-20
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 2019-01-09
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多