char mem[8];
uint64_t *memory{(uint64_t*)(void*)&mem[0]};
std::cout << "diff: " << (void*)memory - (void*)(&mem[0]) << std::endl;
琐碎的例子,gcc的错误信息是:
error: invalid use of ‘void’
std::cout << "diff: " << (void*)memory - (void*)(&mem[0]) << std::endl;
答案 0 :(得分:1)
无效使用void,为什么?
这是因为void*
(存储在此指针处的项目)的大小未知。
如果要计算指针之间的距离(以字节为单位),请将它们转换为1字节大小的字符串(char *,unsigned char *,uint8_t *等):
std::cout << "diff: " << (uint8_t*)memory - (uint8_t*)(&mem[0]) << std::endl;
答案 1 :(得分:0)