Search code examples
carrayspointersc89

Can't compile C89 with array of pointers


I'm trying to compile my C89 code that includes an array of pointers to locations in various other arrays. I keep getting ERROR (207) Aggregate or union objects may be initialized with constant expressions only, probably because of how I'm defining it at initialization but I don't really have a better way to do it except defining it one index at a time, which I'd prefer to avoid. I've tried everything I can think of, is there any way to get around defining it one line at a time?

uint8_t * keyPtrs[] = {
    towers,
    towers,
    *(towers + 1),
    *(towers + 1),
    *(towers + 2),
    *(towers + 2),
    allianceStack
};

Solution

  • In C89 the initializers for an array must be computable at compile-time. See here for a more detailed definition.

    In later versions of the language standard this requirement was relaxed for automatic arrays (but remains for static arrays).

    You will have to use assignment statements, or otherwise, to fill in values that are not known until runtime.