0

From Kerrek SB's comment on Why can't a modifiable lvalue have an array type?

You can trivially assign arrays by making them members of a struct and then assigning those lvalues.

What does it mean? Does it mean that if a structure has an array member, then the array member can be modifiable? But the following example doesn't work:

I define a struct type with a member being an array

typedef struct { int arr[3];} MyType;

Then

MyType myStruct;
myStruct.arr = (int[]) {3,2,1};

and got error: assignment to expression with array type.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Tim
  • 1
  • 141
  • 372
  • 590
  • For your code, you'd need to use: `MyType myStruct; myStruct.arr = (MyType) { { 3, 2, 1 } };` or `myStruct.arr = (MyType) { .arr = { 3, 2, 1 } };` to create a compound literal of the structure type, which can then be assigned to the structure as a whole. You still can't do array assignments, so any assignment to `myStruct.arr` is bogus. – Jonathan Leffler Aug 14 '17 at 23:02

1 Answers1

4

No, it means that if you assign an instance of a struct like the one in your example to another struct you are effectively assigning arrays.

struct Array {
    int array[3];
};

struct Array A;
struct Array B = {{0, 1, 2}}; // by Chris Dodd
// This is valid!
A = B;
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97