請幫助我瞭解兩件事我在C代碼中發現:Ç請幫忙解釋一下小碼
首先,是整個代碼:
usbMsgLen_t usbFunctionSetup(uchar data[8])
{
usbRequest_t *rq = (void *)data;
static uchar dataBuffer[4]; /* buffer must stay valid when usbFunctionSetup returns */
if(rq->bRequest == CUSTOM_RQ_ECHO){ /* echo -- used for reliability tests */
dataBuffer[0] = rq->wValue.bytes[0];
dataBuffer[1] = rq->wValue.bytes[1];
dataBuffer[2] = rq->wIndex.bytes[0];
dataBuffer[3] = rq->wIndex.bytes[1];
usbMsgPtr = dataBuffer; /* tell the driver which data to return */
return 4;
}else if(rq->bRequest == CUSTOM_RQ_SET_STATUS){
if(rq->wValue.bytes[0] & 1){ /* set LED */
LED_PORT_OUTPUT |= _BV(LED_BIT);
}else{ /* clear LED */
LED_PORT_OUTPUT &= ~_BV(LED_BIT);
}
}else if(rq->bRequest == CUSTOM_RQ_GET_STATUS) {
dataBuffer[0] = ((LED_PORT_OUTPUT & _BV(LED_BIT)) != 0);
usbMsgPtr = dataBuffer; /* tell the driver which data to return */
return 1; /* tell the driver to send 1 byte */
}
return 0; /* default for not implemented requests: return no data back to host */
}
現在,usbFunctionSetup
得到的8個無符號字符數組。現在有線:
usbRequest_t *rq = (void *)data;
所以,我得到了左側的聲明,但是什麼是右側?我知道(void *)
被轉換爲這種類型,但爲什麼?
而第二個問題是,心不是這個代碼效率低下?因爲第一個函數接收8個字節的數據,並且它爲它們創建了額外的指針。並且創建額外的指針,至少如果我是正確的,只是爲了能夠通過在usbRequest_t
結構中定義的名稱來訪問單個數據。那豈不是更簡單,更高效的只是在代碼中使用,而不是僅僅rq->bRequest == something
例如 data[2]==something
或者bRequest
比一個字節大,例如data[1] == low_byte_of_something && data[2]== high_byte_of_something
?