简体   繁体   中英

c char string and stdint.h uint8_t compatability in embedded system

I need to convert const char[] to uint8_t in C++. What is the best method

a)

const uint8_t* a = (const uint8_t*)"string";  // I hate this cast, but it works

b)

const uint8_t* a = reinterpret_cast<const uint8_t*>("string");  // is this OK, is it safe?

Why will this not work?

const uint8_t* a = static_cast<const uint8_t*>("string");  // will not compile

The second solution is the "Most Correct" way to do it. The reinterpret_cast will be handled entirely by the compiler and simply point at the resulting bits with a different type. The first solution is old-style and generally should not be used in modern C++.

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