我有一个未签名的 C++ 字符缓冲区
unsigned char* pBuffer = new unsigned char [1024];
我想通过使用赋值而不是 memcpy 将 pBuffer
指针保存在新分配的缓冲区的前几个字节中
*(unsigned char*) &pBuffer[0] = pBuffer;
我无法获得正确的语法。感谢您提供正确语法的帮助。
Visual Studio 2010
最佳答案
我不知道你为什么要这样做,但我能想到一些奇怪的原因,所以:
reinterpret_cast<unsigned char**>(pBuffer)[0] = pBuffer;
您希望编译器将指针重新解释为 unsigned char**
,并将 pBuffer
的值存储在 0-index 槽中。
您说您正在为一个包含多个 pBuffer 的“链表”编制索引。在这种情况下,您不应使用 reinterpret_cast。相反:
struct node {
node* prev;
std::unique_ptr<node> next;
unsigned char buffer[1024];
node(node* prev_) : prev(prev_) {}
};
std::unique_ptr<node> list(new node(nullptr)); //tada
关于c++ - 复制指针值c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9688557/