Invalid conversion from ‘void*’ to ‘unsigned char*’

You need to cast as you can not convert a void* to anything without casting it first.

You would need to do

unsigned char* etherhead = (unsigned char*)buffer;

(although you could use a static_cast also)

To learn more about void pointers, take a look at 6.13 — Void pointers.


The "type-less" state of void* only exist in C, not C++ with stronger type-safety.


A void* might point at anything and you can convert a pointer to anything else to a void* without a cast but you have to use a static_cast to do the reverse.

unsigned char* etherhead = static_cast<unsigned char*>(buffer);

If you want a dynamically allocated buffer of 100 unsigned char you are better off doing this and avoiding the cast.

unsigned char* p = new unsigned char[100];

You can convert any pointer to a void *, but you can't convert void * to anything else without a cast. It might help to imagine that "void" is the base class for EVERYTHING, and "int" and "char" and whatnot are all subclasses of "void."