gpt4 book ai didi

c++ - 如何在 C++ 中正确访问 unsigned char 指针的数据?

转载 作者:太空狗 更新时间:2023-10-29 20:23:36 26 4
gpt4 key购买 nike

我需要编写一系列类来模拟一些硬件行为。为了与程序的其余部分正确交互,我的类(class)需要使用 unsigned char * bb 作为输入。因为在创建以下测试程序之前我从未使用过这种特定类型,只是为了了解如何访问/解释数据:

// This will be the test bench
int main() {

unsigned char * bytebuffer = new unsigned char[100];
bytebuffer = (unsigned char *) 548;
InstructionDeco deco(bytebuffer);
return 0;
}

构造函数有:

InstructionDeco::InstructionDeco(unsigned char *bb){
cout << "Hola" << endl;
if (bb){
cout << "Not NULL" << endl;
}
else{
cout << "Si es NULL" << endl;
}
cout << "El BB es " << bb << endl;
}

这告诉我指针不是 NULL,但无论我如何更改行

bb << endl

对于 bb[0] 或 *bb 或 *bb[0] 我总是在该行遇到段错误。我在想我不知道如何正确访问数据。

我做错了什么?

最佳答案

您正在尝试取消引用指向地址 548 的指针,这是一个无效地址。

关于c++ - 如何在 C++ 中正确访问 unsigned char 指针的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32248856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com