0

When creating a struct and initializing it like so in file foo.h: struct t_foo{int a, int b}foo{1, 2}; And then using it in another file foomap.h: (addr is implemented as #define addrX VAL )

struct foomap_item
{
int addr
int read;
int write;
}

struct foomap_item foomap[]{
{addr1, foo.a, 0   },
{addr2, 0,     foo.b }
}

I get an error "Initializiner element is not constant", because fields in my struct foo is not actually a constant. The point of this is to initialize "map" and have the ability to change fields in my foomap (read and write).

I believe that something like two or three additional hashmaps will work but I don't understand which variable and where put on right now.

providerZ
  • 315
  • 11
  • 1
    First thing you do in the `main` function, *assign* to the elements of `foomap`? You can use [compound literals](https://en.cppreference.com/w/c/language/compound_literal) to create the structure objects and assign directly to each element of the `foomap` array. – Some programmer dude Dec 06 '22 at 07:48
  • There's no good reason why you would declare these variables at file scope unless they are also `static`. – Lundin Dec 06 '22 at 07:51
  • you can add `const` quantifier to the declaration of `foo`. It should help for gcc/clang which have an extension that accepts `const` objects as constant expressions. – tstanisl Dec 06 '22 at 12:37

0 Answers0