[英]Type casting between char* and UBYTE* (unsigned char*)
Background: I am receiving a array as char* as a part of socket session. 背景:作为套接字会话的一部分,我收到的数组为char *。 Now we have to match the Tokens (HTTP headers) out of it.Code here is that we have created a UBYTE* and getting the value from the char array after typecasting with UBYTE.
现在我们必须从中匹配Tokens(HTTP标头)。这里的代码是我们创建了一个UBYTE *,并在用UBYTE进行类型转换后从char数组中获取值。 Later same UBYTE pointer we are passing to other function which accepts char* after typecasting it to char*.
稍后,我们将传递给另一个函数的UBYTE指针传递给将char *类型转换为char *之后接受char *的其他函数。
Problem here is this works in release build and not in debug build (with -g and different optimization). 这里的问题是这在发行版本中有效,而在调试版本中不起作用(使用-g和不同的优化方法)。 Not only this adding few prints in debug mode hide the problem.
在调试模式下添加少量打印件不仅隐藏了问题。
So my queestion here, What is the difference between UByte pointer(which is essentially a unsigned char) and char pointer. 所以我的问题是,UByte指针(本质上是无符号字符)和char指针之间有什么区别? Changing UByte to char is solving my problem in all modes but I dont have any explaination for the same?
将UByte更改为char可以在所有模式下解决我的问题,但是我对此没有任何解释吗? Any thoughts ?
有什么想法吗 ?
There is nothing wrong with casting between char *
and unsigned char *
. char *
和unsigned char *
之间的强制转换没有问题。 If you're getting unexpected behavior which varies depending on optimization levels, there's certainly a bug in your code, but it probably has little to do with discarding signedness in the cast. 如果您遇到意想不到的行为(具体取决于优化级别),那么代码中肯定存在一个错误,但可能与放弃强制转换中的签名无关。
Aside from that, UBYTE
is a pretty ridiculous typedef since there exists a standard C type, uint8_t
, which is identical and defined in stdint.h
. 除此之外,
UBYTE
是一个非常荒谬的typedef,因为存在一个标准的C类型uint8_t
,它与stdint.h
定义的相同。
Perhaps you could explain in the first place, why you though you had to use an unsigned char
in the first place? 也许您可以首先解释一下,尽管为什么您必须首先使用
unsigned char
? And what doesn't work means? 什么是行不通的呢?
void*
, char*
and unsigned char*
have different semantics and you should use them according to that: void*
, char*
和unsigned char*
具有不同的语义,应根据以下条件使用它们:
void*
points to unspecific data with which you can't do anything unless you cast it to some real type void*
指向非特定数据,除非将其转换为某种实类型,否则您将无法执行任何操作 char*
unfortunately has two different meanings, either as text string, or as unspecific data but which may be addressed (patched) at a low (byte) level char*
具有两种不同的含义,要么是文本字符串,要么是非特定数据,但是可以以低(字节)级别进行寻址(修补)。 signed char
and unsigned char
are small width integers on which you want to perform arithmetics signed char
和unsigned char
是要在其上执行算术的小宽度整数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.