Dereferencing void pointers

青春壹個敷衍的年華 提交于 2019-11-30 18:35:23

buffer->elements is also a void * so you need to cast it before you can do anything with it:

*(char*)element = ((char *)buffer->elements)[buffer->start];

Given all that, how do I tell the compiler that the content of the memory at that address is a char, and that it is okay to dereference it?

Well, you've already done it on the LHS of that line:

*(char*)element = *(buffer->elements[buffer->start]);

To derefence buffer->elements[n] you will need to cast that as well.

*(char*)element = *((char*)buffer->elements)[buffer->start];

Now the question is whether or not that cast is correct. I can't tell you that as you did not post the initialization of buffer->elements.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!