简体   繁体   中英

How to convert const void* to unsigned char*?

为了将const void *转换为无符号char *,C转换使用什么C ++转换?

auto ucharptr = (unsigned char*)const_void_ptr;

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);

尝试使用C ++强制转换运算符,您需要使用它们中的两个 :一个用于删除const ,另一个用于强制转换为指针类型:

auto ucharptr = reinterpret_cast<unsigned char*>(const_cast<void*>(const_void_ptr));

最简单的方法可能是以下方法:

unsigned char* ucharptr = reinterpret_cast<unsigned char*>(const_cast<void*>(const_void_ptr));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM