In C++, I'm getting a message “error: 'void*' is not a pointer-to-object type”

天涯浪子 提交于 2019-12-02 08:13:42

As the compiler message says, void* is not a pointer to object type. What this means is that you cannot do anything with void*, besides explicitly converting it back to another pointer type. A void* represents an address, but it doesn’t specify the type of things it points to, and at a consequence you cannot operate on it.

The compiler needs the type of the variable to to dereference the pointer.

only example no malloc: *int myPtnr = 0x12345;

When you write

*myPtr = NUMBER:

The compiler looks at the type and say .. okay here we have a int ... the Information i need are in the next 4 bytes starting with the adress of the pointer.

Thats the reason why you have to tell the compiler the type. When you use void the compiler dont know how much bytes he has to use for dereference.

you cant derefrence void*, and that is what the coder is doing.

*x = 23; // this cant be done with void*

instead :

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