On 10/10/2016 19:44, Michael Felt wrote:
+11 main() +12 { +13 http_auth_param_t b[] = { +14 (http_auth_param_t) { .p1 = "c1" }, +15 (http_auth_param_t) { .p2 = "e2" } +16 }; +17 +18 printf("%s\n", a[0].p1); +19 printf("%s\n", b[1].p2); +20 }
Updated to:
+1 typedef struct {
+2 char * p1;
+3 char * p2;
+4 } http_auth_param_t;
+5
+6 http_auth_param_t a[] =
+7 { "a1", "a2",
+8 "b1", "b2"
+9 };
+10
+11 struct xxx {
+12 char *lbl;
+13 http_auth_param_t a[];
+14 };
+15 struct xxx X = (struct xxx) {
+16 .lbl = "labelX",
+17 .a = {
+18 (http_auth_param_t) { .p1 = "c1" },
+19 (http_auth_param_t) { .p2 = "g2" },
+20 (http_auth_param_t) { }
+21 }
+22 };
+23 main()
+24 {
+25 http_auth_param_t b[] = {
+26 (http_auth_param_t) { .p1 = "c1" },
+27 (http_auth_param_t) { .p2 = "e2" },
+28 (http_auth_param_t) { }
+29 };
+30
+31 printf("%s\n", a[0].p1);
+32 printf("%s\n", b[1].p2);
+33 printf("%s\n", X.a[1].p2);
+34 }
!cc c99_comp_literal.c; ./a.out a1 e2 g2
The key element seems to be in the struct definition:
this works:
+11 struct xxx {
+12 char *lbl;
+13 http_auth_param_t a[];
+14 };
but
+11 struct xxx {
+12 char *lbl;
+13 http_auth_param_t *a;
+14 };
!cc c99_comp_literal.c; ./a.out "c99_comp_literal.c", line 18.20: 1506-196 (S) Initialization between types "struct {...}*" and "struct {...}" is not allowed. "c99_comp_literal.c", line 19.20: 1506-026 (S) Number of initializers cannot be greater than the number of aggregate members. "c99_comp_literal.c", line 20.20: 1506-026 (S) Number of initializers cannot be greater than the number of aggregate members.
I (am guessing) think the reason is because *a could be an array of "random"(ized) pointers pointing to single instance, while [] says it will be an array of structs.