c – 将char数组转换为unsigned char *

前端之家收集整理的这篇文章主要介绍了c – 将char数组转换为unsigned char *前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法将char []转换为unsigned char *?
char buf[50] = "this is a test"; 
unsigned char* conbuf = // what should I add here

解决方法

虽然它在技术上可能不是100%合法,但这将起作用reinterpret_cast< unsigned char *>(buf).

这不是100%技术合法的原因是由于5.2.10 expr.reinterpret.cast bullet 7.

A pointer to an object can be explicitly converted to a pointer to an object of a different type. original type yields the original pointer value,the result of such a pointer conversion is unspecified.

我认为* reinterpret_cast< unsigned char *>(buf)=’a’未指定,但* reinterpret_cast< char *>(reinterpret_cast< unsigned char *>(buf))=’a’是正常的.

原文链接:https://www.f2er.com/c/111013.html

猜你在找的C&C++相关文章