Example on C++ data member aligment
<p>I have tried to implement Stack&nbsp;Overflow question <em><a href="http://stackoverflow.com/questions/1676385/c-data-member-alignment-and-array-packing">C++ Data Member Alignment and Array Packing</a></em>. Here is the code:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stddef.h&gt;
typedef struct {
    unsigned char a;
    unsigned char b;
    unsigned char c;
}foo;

typedef struct{
    unsigned short i;
    unsigned char a;
    unsigned char b;
    unsigned char c;
} Bar;

typedef struct {foo f[5];} f_b;
typedef struct {Bar[5];} b_f;
#define ALIGNMENT_OF(t) offsetof( struct { char x; t test; }, test )

int main(void){
    printf("Foo:: Size: %d; Alignment: %d\n", sizeof(foo), ALIGNMENT_OF(foo));
    printf("Bar:: Size: %d; Alignment: %d\n", sizeof(Bar), ALIGNMENT_OF(Bar));
    printf("F_B:: Size: %d; Alignment: %d\n", sizeof(f_b), ALIGNMENT_OF(f_B));
    printf("B_F:: Size: %d; Alignment: %d\n", sizeof(b_f), ALIGNMENT_OF(b_f));
    return 0;
}
</code></pre>

<p>But here are the mistakes:</p>

<pre><code>1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(16): error C2059: syntax error : '['
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(16): error C2238: unexpected token(s) preceding ';'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2332: 'struct' : missing tag name
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing '(' before ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2143: syntax error : missing ';' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2227: left of '-&gt;test' must point to class/struct/union/generic type
1&gt;          type is 'int'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(19): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2332: 'struct' : missing tag name
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing '(' before ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2143: syntax error : missing ';' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2227: left of '-&gt;test' must point to class/struct/union/generic type
1&gt;          type is 'int'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(20): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2332: 'struct' : missing tag name
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing '(' before ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2143: syntax error : missing ';' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2065: 'f_B' : undeclared identifier
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2146: syntax error : missing ';' before identifier 'test'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2227: left of '-&gt;test' must point to class/struct/union/generic type
1&gt;          type is 'int'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(21): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2332: 'struct' : missing tag name
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing '(' before ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ')' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2143: syntax error : missing ';' before '{'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2227: left of '-&gt;test' must point to class/struct/union/generic type
1&gt;          type is 'int'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
1&gt;c:\users\david\documents\visual studio 2010\projects\data_packing\data_packing.cpp(22): error C2059: syntax error : ')'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
</code></pre>

<p>But in code it does not show me a red line which indicates an error. How do I fix this problem?</p>
