我有一個 C 函式:
* \fn int32_t readMyData(uint8_t *dStatus, uint8_t *dData, uint8_t *dCRC)
* \param *dStatus pointer to address where STATUS byte will be stored
* \param *dData pointer to starting address where data bytes will be stored
* \param *dCRC pointer to address where CRC byte will be stored
* \return 32-bit sign-extended conversion result (data only)
int32_t readMyData(uint8_t status[], uint8_t data[], uint8_t crc[])
我不習慣指標,你能幫我,我應該在我的 Main 函式中初始化什么樣的變數才能呼叫 readMyData?在原型的引數中,它是陣列:uint8_t status[], uint8_t data[], uint8_t crc[] 但在函式的注釋中它指向: dStatus 指向地址的指標。
我應該定義:
uint8_t *status, *data, *crc
int32_t result =0;
然后呼叫函式;
result = readMyData(&status,&data,&crc);
有道理嗎?
謝謝
uj5u.com熱心網友回復:
如果檔案/規范指出引數是指標,則應使用指標而不是陣列宣告(盡管不是錯誤而是語意)。
所以你的函式原型應該在檔案中看起來像:
int32_t readMyData(uint8_t *dStatus, uint8_t *dData, uint8_t *dCRC);
此外,引數的描述指出,這些是指向將存盤特定資料的地址的指標,因此如果要呼叫該函式,則必須提供/定義該存盤。
例如
uint8_t dStatus; //single byte, no array needed
uint8_t dData[NUM_DATA]; //NUM_DATA some arbitrary number
uint8_t dCRC[NUM_CRC]; //NUM_CRC some arbitrary number
//invoke
int32_t result = readMyData(
&dStatus /* usage of address of operator */,
dData /* no usage of & needed, array decays to pointer */,
dCRC /* same as dData */
);
陣列到指標轉換的更多資訊:
- 陣列到指標的轉換 - cppreference.com
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327481.html
上一篇:Python到C浮點不精確
