| [65582ca] | 1 | #include <assert.h>
|
|---|
| 2 |
|
|---|
| 3 | struct T {
|
|---|
| 4 | int w;
|
|---|
| 5 | struct {
|
|---|
| 6 | int x;
|
|---|
| 7 | int y;
|
|---|
| 8 | };
|
|---|
| 9 | int z;
|
|---|
| 10 | };
|
|---|
| 11 |
|
|---|
| 12 | union U {
|
|---|
| 13 | int w;
|
|---|
| 14 | struct {
|
|---|
| 15 | int x;
|
|---|
| 16 | int y;
|
|---|
| 17 | };
|
|---|
| 18 | int z;
|
|---|
| 19 | };
|
|---|
| 20 |
|
|---|
| 21 | int c[10] = {[2]=3};
|
|---|
| 22 |
|
|---|
| 23 | int main() {
|
|---|
| 24 | struct T a0 = {1, 2, 3, 4};
|
|---|
| 25 | struct T a1 = {1, {2, 3}, 4};
|
|---|
| 26 | struct T a2 = {1, {2, 3}}; // .w = 1 .x = 2 .y = 3
|
|---|
| 27 | /* struct T a3 = {{2, 3}, 4};
|
|---|
| 28 | *
|
|---|
| 29 | * The brace is not matching the sub-struct correctly. C11
|
|---|
| 30 | * Sec. 6.7.9 (20) describes such situation. My understand of C11
|
|---|
| 31 | * is that "{2, 3}" will be used to initialize the first named
|
|---|
| 32 | * member which is w. Unnamed members will not be initialized.
|
|---|
| 33 | *
|
|---|
| 34 | * CIVL reports error for this case while both GCC and Clang accepts.
|
|---|
| 35 | */
|
|---|
| 36 | /* struct T a4 = {.x = 1, .y = 2, .x = 3};
|
|---|
| 37 | * Can unnamed members be initialized with designators ?
|
|---|
| 38 | * CIVL reports error for this case while both GCC and Clang accepts.
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | assert(a2.y == 3 && a2.z == 0);
|
|---|
| 42 | // assert(a3.w == 2 && a3.x == 4);
|
|---|
| 43 | // assert(a4.x == 3 && a4.y == 2);
|
|---|
| 44 |
|
|---|
| 45 | union U b0 = {4, 2, 3, 1}; // start from the first member .w = 1, ignore rest
|
|---|
| 46 | // same as above, GCC doesn't accepts this though according to C11 it is acceptable, CIVL accpets this:
|
|---|
| 47 | union U b1 = {1, {2, 3}, 4};
|
|---|
| 48 | // CIVL reports error for the followings:
|
|---|
| 49 | // union U b2 = {{2, 3}, 4}; brace unmatch, CIVL reports error for this
|
|---|
| 50 | // union U b3 = {{2, 3}}; same as above
|
|---|
| 51 | // union U b4 = {.x = 1, .z = 2};// override to .z = 2
|
|---|
| 52 |
|
|---|
| 53 | assert(b0.w == 4);
|
|---|
| 54 | assert(b1.w == 1);
|
|---|
| 55 | //assert(b2.w == 2);
|
|---|
| 56 | //assert(b3.w == 2);
|
|---|
| 57 | //assert(b4.z == 2);
|
|---|
| 58 | }
|
|---|