tom sharon tom sharon - 9 months ago 55
C Question

expression must be a modifiable lvalue on a struct array that is modifiable

i have a program that is supposed to stimulate the compiler of a certain language, using structs i made each "word" of memory have a 15 int array of "bits", the problem is that every time i try to compile this test i get 2 errors:

expression must be a modifiable lvalue

and
expected an expression

I have no idea why the second one appears,
and when I searched the first error I found that the left value being non-modifiable even though I cant see why.

note: I tried to use a pointer instead of an array and it didn't change a thing

code:

word memory[1000];

struct bit {
unsigned int bt : 1;
}typedef bit;

struct word {
bit mem[15];
}typedef word;

memory[0].mem = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };


edit: please do not suggest changes to the structs because they are part of a big program and it will take too much work to change them

Answer Source

Arrays are not directly assignable in C. However, structs containing arrays are, so what you can do is this:

word w = { { {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1} } };
memory[0] = w;

With C99 you can use a compound literal and say:

memory[0] = (struct word){ { {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1}, {1} } };

(In C++ you could use unconstrained temporary values.)