我有一个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);
有意义吗?
谢谢
如果文档/规范声明实参是指针,则应该使用指针而不是数组声明(虽然不是错误,但具有语义)。
所以你的函数原型应该在文档中看起来像:
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