I am trying to make a function, that lets me look in an array of structs, and return the one whose value of a certain member matches the input. I found another thread, where a guy had a familiar issue, but it doesn't seem to work for me. The other thread can be found here: Get a struct based on member values
I have the struct:
typedef struct
{
uint8_t register_id;
uint8_t size; //In bytes
char * name;
char * access;
} Register;
I've made an array of these:
Register VN100_registers[4] = {
{9, 16, "Attitude Quaternion", "read only"},
{17, 12, "Magnetic measurements", "read only"},
{18, 12, "Acceleration measurements", "read only"},
{19, 12, "Angular rate measurements", "read only"},
};
I use this function:
static Register* GetVN100Register(const void* value_ptr, const size_t offset, const size_t field_size)
{
for (uint32_t i = 0; i < N_ELEMS(VN100_registers); i++)
{
if (0 == memcmp(value_ptr, ((unsigned char*)&VN100_registers[i])+offset, field_size))
{
return &VN100_registers[i];
}
}
return NULL;
}
It is supposed to point to the struct in the array VN100_registers, where a member value matches function input.
I initialize the variables:
Register *test_ptr;
uint32_t value_to_find;
And call the function:
value_to_find = 18;
test_ptr = GetVN100Register(&value_to_find, offsetof(Register, register_id), sizeof(value_to_find));
I should be pointed to the third struct of the array, but when I debug, I get this: