简体   繁体   English

我应该向这个函数发送什么参数

[英]What arguments should I send to this function

I have a C function:我有一个 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[])

I am not used to pointer, could you help me, what kind of variables should I initialize in my Main function to be able to to call readMyData?我不习惯指针,你能帮我,我应该在我的 Main 函数中初始化什么样的变量才能调用 readMyData? In the arguments of the prototype it is Arrays: uint8_t status[], uint8_t data[], uint8_t crc[] but in the comments of the function it pointers: dStatus pointer to address.在原型的参数中,它是数组:uint8_t status[], uint8_t data[], uint8_t crc[] 但在函数的注释中它指向: dStatus 指向地址的指针。

Should I define:我应该定义:

uint8_t *status, *data, *crc
int32_t result =0;

Then call the function;然后调用函数;

result = readMyData(&status,&data,&crc);

Does it makes sense?有道理吗?

Thank you谢谢

If the documentation/specification states that the arguments are pointers, then pointer and not array declaration should be used (although not an error but semantics).如果文档/规范指出参数是指针,则应使用指针而不是数组声明(尽管不是错误而是语义)。

So your function prototype should look like in the documentation:所以你的函数原型应该在文档中看起来像:

int32_t readMyData(uint8_t *dStatus, uint8_t *dData, uint8_t *dCRC);

Further, the description for the arguments states, that these are pointers to addresses where the specific data will be stored , so if you want to call that function, you have to provide/define that storage .此外,参数的描述指出,这些是指向将存储特定数据的地址的指针,因此如果您想调用该函数,您必须提供/定义该存储

eg例如

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 */
);

enter code here Thank you very much Erdal, Weird that the documentation gives: enter code here非常感谢 Erdal,文档给出的奇怪之处:

 * \fn int32_t readData(uint8_t *dStatus, uint8_t *dData, uint8_t *dCRC)

but the function prototype and the function are :但函数原型和函数是:

int32_t readData(uint8_t status[], uint8_t data[], uint8_t crc[]);

So if I don't want to change anything in the library and the function, I would need to send Arrays:因此,如果我不想更改库和函数中的任何内容,则需要发送数组:

uint8_t dStatus[1];         //single byte, no array needed
uint8_t dData[10]; //NUM_DATA some arbitrary number
uint8_t dCRC[4];   //NUM_CRC some arbitrary number

//invoke
int32_t result = readMyData(dStatus,dData,dCRC);

Is it ok?可以吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该分配一个带有参数的函数给带空的函数指针吗? - Should I assign a function that takes arguments to function pointers that take a void? 我应该在'gamma' function 中更改什么? - What should I change in the 'gamma' function? 函数未收到以const char形式发送的内容* - Function is not receiving what I send as a const char * 如果我用一个空参数表声明一个函数,然后传递参数怎么办? - What if I declare a function with empty parameter table, then pass arguments to it? 如何使用此“printf”函数减少参数数量? 我应该使用“printf”以外的其他东西吗? - How can I reduce the number of arguments with this "printf" function? Should I be using something other than "printf"? 什么是localtime函数的有效参数? - What are valid arguments for localtime function? pthread 函数参数应该在堆栈还是堆上分配? - Should pthread function arguments be allocated on the stack or the heap? 在C语言中,我应该定义(而不是声明/原型)不带参数或带空列表或空列表的函数吗? - In C, should I define (not declare/prototype) a function that takes no arguments with void or with an empty list? 我应该包含哪些 header 文件才能使用 flock function? - What header files should I include to use the flock function? 我应该在我的函数中改变什么才能计算实数? - what should i change in my function so it calculates real numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM