title: Compiler Warning when using Pointers to Packed Structure Members
body: <p>Many C/C++ compilers (including gcc and clang) have a feature called packed structures. It comes in handy for a number of reasons, but it has to be used with caution. One potential pitfall is that you use a pointer to a member of a struct as an argument of another function. Now that function is unaware of the unaligned pointer. Let me illustrate what I mean with some code:</p>
  
  <pre><code>#pragma pack(1)
  typedef struct { int x; } uas;
  #pragma pack()
  
  void foo(int *f) {
    // some code using the value of *f
  }
  void bar(uas *b) {
    foo(&amp;(b-&gt;x));
  }
  </code></pre>
  
  <p>The alignment of <code>int</code> on a 32 bit machine is usually 4. The compiler now may generate code for <code>foo()</code> that may not work if <code>f</code> is not 4 byte aligned. This is the case on older ARM architectures.</p>
  
  <p>Now struct <code>uas</code> and all members within have an alignment guarantee of 1. Clearly, passing the address of <code>b-&gt;x</code> to <code>foo()</code> is a bad idea.</p>
  
  <p>GCC and clang have a compiler warning (<code>-Wcast-align</code>) which is triggered, for example, by casting <code>char*</code> to <code>int*</code>. Using pointers to members of packed structures, even though supported by both, doesn't seem to trigger this warning. I also tried <code>-Wall</code> and <code>-Wextra</code>, but they do not even include <code>-Wcast-align</code>.</p>
  
  <p>My main question is whether GCC, clang, or any other compiler supporting packed structures have a warning that would be triggered by the particular example above. It seems, such a warning is mandatory if compilers support packed structures.</p>