vendredi 22 juillet 2016

dereferencing an array in a struct (in C)

Let's say I have a struct and the following functions:

struct in header file:

typedef struct {
uint8_t length;
uint8_t data[8];
} simple_vector;

function:

simple_vector* SD_cmd(uint8_t cmd, uint32_t arg, uint8_t crc, uint8_t read_cnt){

simple_vector vector;

//stuff happens and the simple_vector is manipulated

return &vector;
}

How do I go about accessing the non-zeroth element of an array using the -> operator?

Let's say a function returns a pointer to this struct:

simple_vector *response;
response = SD_cmd(8, 0x000001AA, 0x87, 5);
uint8_t value = response->data[3];

The last line in the above snippet does not return the correct value. I've debugged (on an mcu) and verified that the data[3] in the function is not the same as what I get when I dereference it outside of the function. Any thoughts?

Aucun commentaire:

Enregistrer un commentaire