Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the following code;

void* buffer = operator new(100);
unsigned char* etherhead = buffer;

I'm getting the following error for that line when trying to compile;

error: invalid conversion from ‘void*’ to ‘unsigned char*’

Why do I get that error, I thought a void was "type-less" so it can point at anything, or anything can point to it?

like image 813
jwbensley Avatar asked Apr 14 '12 20:04

jwbensley


1 Answers

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.

like image 157
josephthomas Avatar answered Sep 26 '22 12:09

josephthomas