2

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!

dbush
  • 205,898
  • 23
  • 218
  • 273
P Pp
  • 137
  • 1
  • 6

1 Answers1

3

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]));
dbush
  • 205,898
  • 23
  • 218
  • 273
  • @PPp Glad I could help. Feel free to [accept this answer](https://stackoverflow.com/help/accepted-answer) if you found it useful. – dbush Jul 27 '21 at 18:11