Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert const void* to unsigned char*?

Tags:

c++

What C++ casts is the C cast using in order to convert from a const void* to a unsigned char*?

auto ucharptr = (unsigned char*)const_void_ptr;
like image 381
Bob Bobby Avatar asked Oct 27 '25 16:10

Bob Bobby


2 Answers

Try the C++ casting operator and you need two of them: one to remove the const and another one to cast to your pointer type:

auto ucharptr = reinterpret_cast<unsigned char*>(const_cast<void*>(const_void_ptr));
like image 90
YePhIcK Avatar answered Oct 29 '25 06:10

YePhIcK


Try this:

const void* ptr = "test example";
auto ucharptr = static_cast<const unsigned char*>(ptr);

//to remove the const ness
unsigned char* test = const_cast<unsigned char*>(ucharptr);
like image 22
Pavan Chandaka Avatar answered Oct 29 '25 07:10

Pavan Chandaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!