0

I want to initialise a const struct:

const struct MyStruct MYSTRUCT_DEFAULTS = {
  "prop1",
  "prop2",
  "prop3",
  123,
  456,
  ...
}

However in the above it is impossible to tell which field is which when the struct is large. In C99 I can use { .prop1 = "prop1, ...} syntax but I am not compiling under C99.

I don't believe I can create the struct and then use MYSTRUCT_DEFAULTS.prop1 = "prop1" because this would violate it being const.

Is there a neater way to initialise my struct and be clear which fields are which? I can obviously use comments next to each field but that is error prone when fields are added or removed from the struct.

Flash
  • 15,945
  • 13
  • 70
  • 98
  • 3
    I think the main reason C99 introduced that syntax is to make such code cleaner. You need it while you don't want C99, the answer is probably no(unless with some compiler extension). – Yu Hao May 24 '14 at 10:10
  • I don't get it, why the multi-line comment is more error prone than a "keyword argument" like C99 syntactic sugar? Ofc, I suggest you to use C99 whenever it is possible. (Actually I suggest C11, but that doesn't matter in our case now) – Peter Varo May 24 '14 at 10:11
  • @PeterVaro @YuHau I need to use the `getaddrinfo` system call which appears not to work with `-std=c99`: http://stackoverflow.com/questions/12024703/why-cant-getaddrinfo-be-found-when-compiling-with-gcc-and-std-c99 – Flash May 24 '14 at 10:20
  • 1
    It does work under C99. Btw, I didn't expected this to be a XY-problem ... – alk May 24 '14 at 10:22

1 Answers1

0

I think the comments are your best bet here (even you said, you are not interested in them), otherwise the answer is no, there are no other syntactic sugars before C99:

enter image description here


real text answer:

const struct MyStruct MYSTRUCT_DEFAULTS = {
    /* First property */  "prop1",
   /* Second property */  "prop2",
 /* Last str property */  "prop3",
       /* Some number */  123,
       /* Magical int */  456,
    /* An ellipse? ;) */  ...
};

See? My screenshot is way nicer..

Peter Varo
  • 11,726
  • 7
  • 55
  • 77