[英]What is (uint32_t*)?
I am new to C. I don't understand how to solve the last two lines of the following code, can you explain it?我是C新手,不明白下面代码最后两行怎么解决,能解释一下吗? thank you very much.
非常感谢您。
pBuffcmd = (uint32_t*)&CmdBuffer[CmdBuffer_Index]; pBuffcmd = (uint32_t*)&CmdBuffer[CmdBuffer_Index]; *pBuffcmd = cmd;
*pBuffcmd = cmd;
#DL_SIZE (8*1024L)
#define CMD_FIFO_SIZE (4*1024L)
#define CMD_SIZE (4)/
uint32_t CmdBuffer_Index;
volatile uint32_t DlBuffer_Index;
uint8_t DlBuffer[DL_SIZE];
uint8_t CmdBuffer[CMD_FIFO_SIZE];
void App_WrCoCmd_Buffer(Gpu_Hal_Context_t *phost,uint32_t cmd)
{
#ifdef BUFFER_OPTIMIZATION
uint32_t *pBuffcmd;
if (CmdBuffer_Index >= CMD_FIFO_SIZE)
{
if (CmdBuffer_Index > 0) {
NOP;
}
CmdBuffer_Index = 0;
}
pBuffcmd = (uint32_t*)&CmdBuffer[CmdBuffer_Index];
*pBuffcmd = cmd;
(uint32_t*)
is a cast . (uint32_t*)
是一个演员表。 A cast is an operator that performs a conversion.强制转换是执行转换的运算符。
In this code, &CmdBuffer[CmdBuffer_Index]
is a pointer to a particular element in CmdBuffer
, and the type of that pointer is “pointer to uint8_t
”, also written uint8_t *
.在这段代码中,
&CmdBuffer[CmdBuffer_Index]
是指向CmdBuffer
特定元素的指针,该指针的类型是“指向uint8_t
”,也写作uint8_t *
。 This cast converts it to a pointer to a uint32_t
, also written uint32_t *
.此转换将其转换为指向
uint32_t
的指针,也写作uint32_t *
。
Then *pBuffcmd = cmd;
然后
*pBuffcmd = cmd;
attempts to write the value cmd
to the uint32_t
pointed to by the converted pointer.尝试将值
cmd
写入转换后的指针指向的uint32_t
。
This is bad code.这是糟糕的代码。 The C standard does not guarantee that converting a
uint8_t *
to a uint32_t *
will work. C 标准不保证将
uint8_t *
转换为uint32_t *
会起作用。 Even if that does work, the C standard does not guarantee that using a uint32_t
reference to write to bytes in an array defined with element type uint8_t
will work.即使这样做有效,C 标准也不能保证使用
uint32_t
引用写入使用元素类型uint8_t
定义的数组中的字节会起作用。 It may be this code is designed for a particular C implementation in which that will work, but the desired result could be obtained using standard C code:这段代码可能是为特定的 C 实现而设计的,它可以在其中工作,但可以使用标准 C 代码获得所需的结果:
memcpy(&CmdBuffer[CmdBuffer_Index], &cmd, sizeof cmd);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.