一个字节加空指针?由两个? [英] Increment void pointer by one byte? by two?
本文介绍了一个字节加空指针?由两个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个叫空指针 PTR
。我想通过字节数来增加该值。有没有办法做到这一点?
I have a void pointer called ptr
. I want to increment this value by a number of bytes. Is there a way to do this?
请注意,我想这样做在避免产生更多的变数。
Please note that I want to do this in-place without creating any more variables.
我可以做类似 PTR =(无效*)(+((字符*)PTR));
推荐答案
您不能在空指针,因为指针运算都是在大小所指向的对象。
You cannot perform arithmetic on a void pointer because pointer arithmetic is defined in terms of the size of the pointed-to object.
您可以,但是,转换指针到的char *
,做那个指针运算,然后将其转换回一个无效*
:
You can, however, cast the pointer to a char*
, do arithmetic on that pointer, and then convert it back to a void*
:
void* p = /* get a pointer somehow */;
// In C++:
p = static_cast<char*>(p) + 1;
// In C:
p = (char*)p + 1;
这篇关于一个字节加空指针?由两个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文