title: C vs C++ placing structs in unsigned char buffer
body: <p>Does C have anything similar to C++ where one can place structs in an unsigned char buffer as is done in C++ as shown in the standard <a href="https://timsong-cpp.github.io/cppwp/intro.object#3" rel="nofollow noreferrer">sec. 6.7.2</a></p>
  <pre><code>template&lt;typename ...T&gt;
  struct AlignedUnion {
    alignas(T...) unsigned char data[max(sizeof(T)...)];
  };
  int f() {
    AlignedUnion&lt;int, char&gt; au;
    int *p = new (au.data) int;           // OK, au.data provides storage
    char *c = new (au.data) char();       // OK, ends lifetime of *p
    char *d = new (au.data + 1) char();
    return *c + *d;                       // OK
  }
  </code></pre>
  <p>In C I can certainly memcpy a struct of things(or int as shown above) into an unsigned char buffer, but then using a pointer to this struct one runs into strict aliasing violations; the buffer has different declared type.</p>
  <p>So suppose one would want to replicate the second line in f the C++ above in C. One would do something like this</p>
  <pre><code>#include&lt;string.h&gt;
  #include&lt;stdio.h&gt;
  struct Buffer {
    unsigned char data[sizeof(int)];
  };
  
  int main()
  { 
    struct Buffer b;
    int n = 5;
    int* p = memcpy(&amp;b.data,&amp;n,sizeof(int));
    printf(&quot;%d&quot;,*p);  // aliasing violation here as unsigned char is accessed as int
    return 0;
  }
  </code></pre>
  <p>Unions are often suggested i.e. <code>union Buffer {int i;unsigned char b[sizeof(int)]};</code> but this is not quite as nice if the aim of the buffer is to act as storage (i.e. placing different sized types in there, by advancing a pointer into the buffer to the free part + potenially some more for proper alignment).</p>