.. Even though this looks good to me,
Wait, stop there. Arrays are not assignable. You cannot use an array type variable as LHS operand of assignment operator.
With my emphasis, quoting C11
, chapter §6.5.16
An assignment operator shall have a modifiable lvalue as its left operand.
and, from §6.3.2.1
.... A modifiable lvalue is an lvalue that
does not have array type, does not have an incomplete type, does not have a const-qualified
type, and if it is a structure or union, does not have any member (including,
recursively, any member or element of all contained aggregates or unions) with a const-qualified
type.
You need to either
- Loop over individual array elements and assign them one by one (in case the elements are arrays themselves, use this theory recursively)
- Use
memcpy()
.
That said, "is memcpy preferred over a loop in case of small arrays?" does not have a definitive answer. You need to check the generated assembly code to make sure. With proper optimization enabled, compiler is likely to choose the best within the two most of the cases.