Error "initializer element is not constant" when trying to initialize variable with const

Posted by tomlogic on Stack Overflow See other posts from Stack Overflow or by tomlogic
Published on 2010-06-11T17:55:27Z Indexed on 2010/06/11 18:12 UTC
Read the original article Hit count: 156

Filed under:

I get an error on line 6 (initialize my_foo to foo_init) of the following program and I'm not sure I understand why.

typedef struct foo_t {
    int a, b, c;
} foo_t;

const foo_t foo_init = { 1, 2, 3 };
foo_t my_foo = foo_init;

int main()
{
    return 0;
}

Keep in mind this is a simplified version of a larger, multi-file project I'm working on. The goal was to have a single constant in the object file, that multiple files could use to initialize a state structure. Since it's an embedded target with limited resources and the struct isn't that small, I don't want multiple copies of the source. I'd prefer not to use:

#define foo_init { 1, 2, 3 }

I'm also trying to write portable code, so I need a solution that's valid C89 or C99.

Does this have to do with the ORGs in an object file? That initialized variables go into one ORG and are initialized by copying the contents of a second ORG?

Maybe I'll just need to change my tactic, and have an initializing function do all of the copies at startup. Unless there are other ideas out there?

© Stack Overflow or respective owner

Related posts about c