P Pp
P Pp

Reputation: 249

error: c code: expression must be a modifiable lvalue

I have two different structs in c file, struct A and B:

typedef Struct _A
{
   float arr[4];
}A;

typedef struct _B
{
   float const x;
   float const y;
}B;

 A* objA = (A*)malloc(sizeof(A));
 B* objB = (B*)malloc(sizeof(B));

what I need to do is assign arr values with values from struct B

 objA->arr = {objB->x, objB->y, objB->x, objB->x};  /// getting an error here : expression must be a modifiable lvalue. 

I have memcpy so far, but that ends in another error "expression expected". is there any way to do this?

Thanks in advance!

Upvotes: 2

Views: 242

Answers (1)

dbush
dbush

Reputation: 223739

You can't assign to an array directly. You'll need to either assign to each member individually:

objA->arr[0] = objB->x;
objA->arr[1] = objB->y;
objA->arr[2] = objB->x;
objA->arr[3] = objB->x;

Or use memcpy with a compound literal as the source:

memcpy(objA->arr, (float[4]){objB->x,objB->y,objB->x,objB->x}, sizeof(float[4]));

Upvotes: 3

Related Questions