Home > OS >  What arguments should I send to this function
What arguments should I send to this function

Time:10-20

I have a C function:

 * \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? 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.

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

CodePudding user response:

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.

e.g.

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

More info to array to pointer conversion:

CodePudding user response:

enter code hereThank you very much Erdal, Weird that the documentation gives:

 * \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?

  • Related