title: Finding struct in array of structs from member value in C
body: <p>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: <a href="https://stackoverflow.com/questions/43220558/get-a-struct-based-on-member-values">Get a struct based on member values</a></p>
  <p>I have the struct:</p>
  <pre><code>typedef struct
  {
      uint8_t register_id;
      uint8_t size; //In bytes
      char * name;
      char * access;
  } Register;
  </code></pre>
  <p>I've made an array of these:</p>
  <pre><code>Register VN100_registers[4] = {
                                   {9,  16, &quot;Attitude Quaternion&quot;,      &quot;read only&quot;},
                                  {17, 12, &quot;Magnetic measurements&quot;,     &quot;read only&quot;},
                                  {18, 12, &quot;Acceleration measurements&quot;, &quot;read only&quot;},
                                  {19, 12, &quot;Angular rate measurements&quot;, &quot;read only&quot;},
  
                                  };
  </code></pre>
  <p>I use this function:</p>
  <pre><code>
  static Register* GetVN100Register(const void* value_ptr, const size_t offset, const size_t field_size)
  {
      for (uint32_t i = 0; i &lt; N_ELEMS(VN100_registers); i++)
      {
          if (0 == memcmp(value_ptr, ((unsigned char*)&amp;VN100_registers[i])+offset, field_size))
          {
              return &amp;VN100_registers[i];
          }
      }
      return NULL;
  }
  
  </code></pre>
  <p>It is supposed to point to the struct in the array VN100_registers, where a member value matches function input.</p>
  <p>I initialize the variables:</p>
  <pre><code>Register *test_ptr;
  uint32_t value_to_find;
  </code></pre>
  <p>And call the function:</p>
  <pre><code>
    value_to_find = 18;
  
    test_ptr = GetVN100Register(&amp;value_to_find, offsetof(Register, register_id), sizeof(value_to_find));
  
  </code></pre>
  <p>I should be pointed to the third struct of the array, but when I debug, I get this:</p>
  <p><a href="https://i.stack.imgur.com/z6URd.png" rel="nofollow noreferrer">Debug view</a></p>