title: validating structures - how to pass string to sizeof and offset methods?
body: <p>My program contains auto-generated structures. When starting I need to validate them with "server" information - so I can be sure that my auto-generated structures are up to date. Server and local structures are valid if they are of the same size, contain fields with the same name and size (and type ideally should be validated too).</p>
  
  <p>This is what I wrote so far:</p>
  
  <pre><code>void SchemeValidator::Validate(cg_scheme_desc_t* schemedesc, Logger&amp; logger)
  {
      struct cg_message_desc_t* msgdesc = schemedesc-&gt;messages;
      while (msgdesc)
      {
          struct cg_field_desc_t* fielddesc = msgdesc-&gt;fields;
          char* structName = msgdesc-&gt;name;
          size_t structSize = msgdesc-&gt;size;
          logger.Debug("Message %s, block size = %d", structName, structSize);
          if (strcmp(structName, "orders")) {
              if (sizeof(orders) != structSize) {
                  printf("Validator error, structure 'orders', local size = %d server size = %d!", sizeof(orders), structSize);
                  throw std::exception("Validator error, structure 'orders' wrong size!");
              }
              while (fielddesc)
              {
                  logger.Debug("\tField %s = %s [size=%d, offset=%d]", fielddesc-&gt;name, fielddesc-&gt;type, fielddesc-&gt;size, fielddesc-&gt;offset);
                  if (offsetof(struct orders, fielddesc-&gt;name) != fielddesc-&gt;offset) {
                      throw std::exception("orders structure offset wrong");
                  }
                  // TODO: validate fielddesc-&gt;size == sizeof corresponding field in structure
                  fielddesc = fielddesc-&gt;next;
              }
          } else {
              throw std::exception("Validator error, validation not implemented!");
          }
  
          msgdesc = msgdesc-&gt;next;
      }
  }
  </code></pre>
  
  <p>There are a lot of problems:</p>
  
  <p>I wrote <code>if (strcmp(structName, "orders"))</code> because later i need to use <code>orders</code> in several expressions, including <code>sizeof(orders)</code> and <code>offsetof(struct orders, fielddesc-&gt;name)</code>. But I have a lot of structures and for each of them I have to copy-paste this block. Can I somehow pass string literal to <code>sizeof</code> and <code>offsetof</code> methods or have desired effect some another way?</p>
  
  <p><code>offsetof(struct orders, fielddesc-&gt;name)</code> doesn't work by same reason - second parameter can not be string literal, I receive <code>error C2039: 'fielddesc' : is not a member of 'orders'</code> error</p>
  
  <p>By the same reason I can validate <code>fielddesc-&gt;size</code>.</p>
  
  <p>How can I achieve desired validation without intensive copy-pasting and values hard-coding?</p>